简体   繁体   English

将smarty布尔值转换为人类可读值。('是'/'否'或'真'/'假')

[英]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. 我正在使用Smarty生成一些表,有一次我打印出从php文件传递的变量值。 The problem is that some of these variables are Boolean values and they render as "1" or "". 问题是这些变量中的一些是布尔值,它们呈现为“1”或“”。 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. 使用PHP var_export()函数作为布尔变量的smarty修饰符。 Set the second parameter to true, so var_export() returns the variable representation instead of outputting it. 将第二个参数设置为true,因此var_export()返回变量表示而不是输出它。

To check if your variable is a boolean, use the PHP is_bool() function. 要检查您的变量是否为布尔值,请使用PHP is_bool()函数。

Your Smarty code should look like this: 您的Smarty代码应如下所示:

{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”或“”。

1 and "" aren't boolean values, they are an integer and a string correspondingly. 1""不是布尔值,它们是相应的整数和字符串。

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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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