多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><th colspan="3" align="center">Smarty - the compiling PHP template engine</th></tr><tr><td width="25%" align="left" valign="bottom"><a href="language.function.function.html" accesskey="P">Prev</a></td> <td width="50%" align="center" valign="bottom">Chapter 7. Built-in Functions[第七章.内置函数]</td> <td width="25%" align="right" valign="bottom"><a href="language.function.include.html" accesskey="N">Next</a></td></tr></table> # {if}{elseif}{else} 条件 {if} statements in Smarty have much the same flexibility as PHP if [http://php.net/if] statements, with a few added features for the template engine. Every {if} must be paired with a matching {/if}. {else} and {elseif} are also permitted. All PHP conditionals and functions are recognized, such as ||, or, &&,and, is_array(), etc. If securty is enabled, only PHP functions from $php_functions property of the securty policy are allowed. See the Security section for details. The following is a list of recognized qualifiers, which must be separated from surrounding elements by spaces. Note that items listed in [brackets] are optional. PHP equivalents are shown where applicable. 随着一些特性加入到模版引擎,Smarty的{if}语句与php的if语句一样富有弹性。每一个{if}必须与一个{/if}成对出现,允许使用{else}和{elseif},所有php条件和函数在这里同样适用,诸如||、or、&&、and、is_array()等等。 如果开启安全,只支持符合$php_functions的安全策略属性的php函数。具体细节参考[安全](#)一节。 下面是一串有效的限定符,它们的左右必须用空格分隔开,注意列出的清单中方括号是可选的,在适用情况下使用相应的等号(全等或不全等)。 ![if leseif](https://box.kancloud.cn/2015-09-02_55e669a0ef82b.gif) <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN2742" id="AEN2742"> </a> <b>Example 7.42. {if} statements<br/> 例 7-42.{if}语句</b><strong/> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> {* 同上 *}<br/> {if $name == 'Fred' || $name == 'Wilma'}<br/> ...<br/> {/if}<br/> {* 允许使用圆括号 *}<br/> {if ( $amount &lt; 0 or $amount &gt; 1000 ) and $volume &gt;= #minVolAmt#}<br/> ...<br/> {/if}<br/> {* 可以嵌入函数 *}<br/> {if count($var) gt 0}<br/> ...<br/> {/if}<br/> {* 数组检查 *}<br/> {if is_array($foo) }<br/> .....<br/> {/if}<br/> {* 是否空值检查 *}<br/> {if isset($foo) }<br/> .....<br/> {/if}</p> <p>{* 测试值为偶数还是奇数 *}<br/> {if $var is even}<br/> ...<br/> {/if}<br/> {if $var is odd}<br/> ...<br/> {/if}<br/> {if $var is not odd}<br/> ...<br/> {/if}<br/> {* 测试var能否被4整除 *}<br/> {if $var is div by 4}<br/> ...<br/> {/if}</p> {* 测试发现var是偶数,2个为一组,也就是0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, 等等 *}<br/> {if $var is even by 2}<br/> ...<br/> {/if}<br/> {* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *}<br/> {if $var is even by 3}<br/> ...<br/> {/if} </td> </tr></table><p><strong><span class="PROGRAMLISTING">Example 7.43. {if} with more examples</span><br/> 例 7-43.更多{if}例子</strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">{if isset($name) &amp;&amp; $name == 'Blog'} {* do something *}{elseif $name == $foo} {* do something *}{/if}{if is_array($foo) &amp;&amp; count($foo) &gt; 0} {* do a foreach loop *}{/if}</pre></td> </tr></table></div></td> </tr></table> <table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="language.function.function.html" accesskey="P">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">Home</a></td><td width="33%" align="right" valign="top"><a href="language.function.include.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">{function}<br/> 函数</td><td width="34%" align="center" valign="top"><a href="language.builtin.functions.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">{include}<br/> 包含</td></tr></table>