{if},{elseif},{else} > Smarty的{if}条件判断和PHP的if 非常相似,只是增加了一些特性。 每个{if}必须有一个配对的{/if}. 也可以使用{else} 和 {elseif}. 全部的PHP条件表达式和函数都可以在if内使用,如||, or, &&, and, is_array(), 等等. 如果开启了安全机制,那么只有在$php_functions设置允许的PHP函数才能使用。 参见安全机制。 下面是可用的运算符列表,使用中都会放到元素的中间并且用空格分隔。 注意列表中[方括号]的是可选的,而且还会列出对应PHP的表达式。 <table border="1"> <colgroup> <col align="center"> <col align="center"> <col> <col> <col> </colgroup> <thead><tr> <th align="center">运算符</th> <th align="center">别名</th> <th>语法示例</th> <th>含义</th> <th>对应PHP语法</th> </tr></thead> <tbody> <tr> <td align="center">==</td> <td align="center">eq</td> <td>$a eq $b</td> <td>等于</td> <td>==</td> </tr> <tr> <td align="center">!=</td> <td align="center">ne, neq</td> <td>$a neq $b</td> <td>不等于</td> <td>!=</td> </tr> <tr> <td align="center">&gt;</td> <td align="center">gt</td> <td>$a gt $b</td> <td>大于</td> <td>&gt;</td> </tr> <tr> <td align="center">&lt;</td> <td align="center">lt</td> <td>$a lt $b</td> <td>小于</td> <td>&lt;</td> </tr> <tr> <td align="center">&gt;=</td> <td align="center">gte, ge</td> <td>$a ge $b</td> <td>大于等于</td> <td>&gt;=</td> </tr> <tr> <td align="center">&lt;=</td> <td align="center">lte, le</td> <td>$a le $b</td> <td>小于等于</td> <td>&lt;=</td> </tr> <tr> <td align="center">===</td> <td align="center">&nbsp;</td> <td>$a === 0</td> <td>绝对等于</td> <td>===</td> </tr> <tr> <td align="center">!</td> <td align="center">not</td> <td>not $a</td> <td>非 (一元运算)</td> <td>!</td> </tr> <tr> <td align="center">%</td> <td align="center">mod</td> <td>$a mod $b</td> <td>取模</td> <td>%</td> </tr> <tr> <td align="center">is [not] div by</td> <td align="center">&nbsp;</td> <td>$a is not div by 4</td> <td>取模为0</td> <td>$a % $b == 0</td> </tr> <tr> <td align="center">is [not] even</td> <td align="center">&nbsp;</td> <td>$a is not even</td> <td>[非] 取模为0 (一元运算)</td> <td>$a % 2 == 0</td> </tr> <tr> <td align="center">is [not] even by</td> <td align="center">&nbsp;</td> <td>$a is not even by $b</td> <td>水平分组 [非] 平均</td> <td>($a / $b) % 2 == 0</td> </tr> <tr> <td align="center">is [not] odd</td> <td align="center">&nbsp;</td> <td>$a is not odd</td> <td>[非] 奇数 (一元运算)</td> <td>$a % 2 != 0</td> </tr> <tr> <td align="center">is [not] odd by</td> <td align="center">&nbsp;</td> <td>$a is not odd by $b</td> <td>[非] 奇数分组</td> <td>($a / $b) % 2 != 0</td> </tr> </tbody> </table> {if} 表达式 ``` _{if $name eq 'Fred'}_ Welcome Sir. _{elseif $name eq 'Wilma'}_ Welcome Ma'am. _{else}_ Welcome, whatever you are. _{/if}_ ``` {使用`or`示例} ``` _{if $name eq 'Fred' or $name eq 'Wilma'}_ ... _{/if}_ ``` {与上面一样的效果} ``` _{if $name == 'Fred' || $name == 'Wilma'}_ ... _{/if}_ ``` {也可以使用括号} ``` _{if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#}_ ... _{/if}_ ``` {还可以嵌入PHP函数调用} ``` _{if count($var) gt 0}_ ... _{/if}_ ``` {检查数组} ``` _{if is_array($foo) }_ ..... _{/if}_ ``` {检查变量是否存在} ``` _{if isset($foo) }_ ..... _{/if}_ ``` {测试值是否偶数或奇数} ``` _{if $var is even}_ ... _{/if}_ _{if $var is odd}_ ... _{/if}_ _{if $var is not odd}_ ... _{/if}_ ``` {测试是否为4可除尽} ``` _{if $var is div by 4}_ ... _{/if}_ ```