3

I am writing a plugin, and everything is working fine so far. Some of my source data includes booleans. So I want to be able to test for true or false.

{if show_data}
   <p>This is some data</p>
{/if}

But it won't work. Are booleans not supported or am I doing something wrong?

EDIT:

Here is a sample of how I would like to assign a boolean.

$array = array('show_data' => true);
$str = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $array);

return $str;
3
  • Can you show us what your data array looks like? Are you using ee()->TMPL->parse_variables() to return it in your plugin? Dec 12 '13 at 19:59
  • Seems to be a common question. I'm having the same issues with the 'matrix' plugin. Have you tried comparison operators? {if show_data != "false"} or {if 'show_data' != ""} Also, what version of EE are you using? Dec 12 '13 at 20:57
  • We are using 2.7.2 I am just using a number for now. It sounds like booleans are not supported. Dec 12 '13 at 21:39
-1

If I'm not mistaken, I read that you must return boolean values from plugins and modules as 'TRUE' and 'FALSE'
EE Template Class - Conditional Variables

6
  • 1
    I don't have enough rep to upvote yet, but MB34 is correct. I've had this issue before in the past too.
    – JamesNZ
    Dec 12 '13 at 21:41
  • So a string of 'FALSE' can be used as a boolean then. It looks like a hack. But I'll have to use it for now. Dec 12 '13 at 21:48
  • 1
    Not a hack if that is the way it is supposed to be done per the tool being used.
    – MB34
    Dec 12 '13 at 23:12
  • Sure, it wouldn't be a hack by us EE users, but a hack by the makers of EE. Dec 13 '13 at 19:43
  • 1
    This answer is not correct and these comments are misleading. Please see my answer (and forgive my arrogance!). Jan 16 '14 at 23:07
2

It is not the case that you need to set your booleans as 'true'/'false' strings, it's that your variables array needs to be nested in its own array like this:

$array = array(array('show_data' => true));
$str = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $array);    
return $str;

You'll now find that outputting {show_data} in a template will return 1, and as such can be used in conditionals like so:

{if show_data}It's true{/if}
{if show_data == 0}It's false{/if}
{if show_data == ''}It's false{/if}
{if !show_data}It's false (as of EE 2.9+){/if}

Docs: http://ellislab.com/expressionengine/user-guide/development/usage/template.html#master-variables-array

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.