简体   繁体   中英

Transform smarty Boolean value to human readable value.('Yes'/'No' or 'True'/'False')

I'm using Smarty to generate some tables, and at one point I'm printing out variable values that have been passed from php file. The problem is that some of these variables are Boolean values and they render as "1" or "". This was my attempt to transform these Boolean values to a human readable format. It does not work. How can can I check if a variable is a Boolean value?

{if $val2.$value_index === true}Yes
{else if $val2.$value_index === false}No
{else}{$val2.$value_index->value}{/if}

Use the PHP var_export() function as a smarty modifier for your boolean variables. Set the second parameter to true, so var_export() returns the variable representation instead of outputting it.

To check if your variable is a boolean, use the PHP is_bool() function.

Your Smarty code should look like this:

{if is_bool($val2.$value_index)}
  {$val2.$value_index|var_export:true}
{/if}

I'm not exactly familiar with this, but it looks as though that should be:

{if $val2.$value_index->value === true}Yes
{elseif $val2.$value_index->value === false}No
{else}{$val2.$value_index->value}{/if}

How about:

{if $va2.$value_index}Yes
{else}No
{/if}

The problem is that some of these variables are Boolean values and they render as "1" or "".

1 and "" aren't boolean values, they are an integer and a string correspondingly.

I googled shorthand for if else, and this post came at first. Just in case some one needs a shorthand for:

{if $val2.$value_index === true}Yes
{else if $val2.$value_index === false}No
{else}{$val2.$value_index->value}{/if}

to:

{($val2.$value_index->value) ? 'yes' : (!$val2.$value_index->value) ? 'no' : $val2.$value_index->value}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM