1

I have a template tag that uses $this->EE->TMPL->parse_variables() to create a single item with an array of [items].

Array ( ... [accessRole] => reader [items] => Array ( ) ... )

What should the parsed variables look like if it's empty and what should be it's corresponding template tag if I want to do something different when there are no [items]?

An empty array doesn't seem to work (as shown above) when do {if items} or {if "{items}" == ""}.

Thoughts?

2

This is not intuitive at all, but you need an array containing an empty array:

$variables = array(
  array(
    'accessRole' => 1,
    'items' => array(array()),
  ),
);

return ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $variables);
2
  • Thanks Rob! {if items} ... {/if} gives (which is it trying to trim [0] => Array ( )): A PHP Error was encountered Severity: Warning Message: trim() expects parameter 1 to be string, array given Filename: libraries/Functions.php Line Number: 2584 Dec 10 '13 at 19:54
  • 1
    You're right. The empty array solves the {items}{/items} tag pair parsing, but not the conditional itself. I'll post another answer. Dec 10 '13 at 20:56
2

Upon closer inspection, the conditionals parser does not deal with arrays properly. I think the only thing you can do is use a separate variable to use with conditionals.

// {if items_count > 0}
'items_count' => count($items),

// OR

// {if has_items}
'has_items' => count($items) > 0,
1
  • It might be worth posting a bug report. Seems reasonable that since arrays are handled well by the tag pair parser, it should work with conditionals, too. Dec 11 '13 at 0:12
0

I found setting the array to "" seemed to work. Albeit screwy.

Something like could be done programmatically:

if (is_array($variables['items'] AND empty($variables['items'])) $variables['items'] = ""

for instance.

1
  • I thought this was working. Instead I still get the trim() error as in Rob's answer when {items} has data and the worst part is {if items} always returns false. :( Dec 10 '13 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.