<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函數。具體細節參考[安全](#)一節。
下面是一串有效的限定符,它們的左右必須用空格分隔開,注意列出的清單中方括號是可選的,在適用情況下使用相應的等號(全等或不全等)。

<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 < 0 or $amount > 1000 ) and $volume >= #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) && $name == 'Blog'} {* do something *}{elseif $name == $foo} {* do something *}{/if}{if is_array($foo) && count($foo) > 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>
- Smarty模板編譯引擎
- 序
- 譯序
- I.開始
- 第一章. 什么是Smarty?
- 第二章. 安裝
- II.模板設計者篇
- 第三章.基本語法
- 注釋
- 變量
- 函數
- 屬性
- 雙引號里嵌入變量
- 數學運算
- 忽略Smarty解析
- 第四章.變量
- 從PHP分配的變量
- 從配置文件讀取的變量
- 變量范圍
- {$smarty}保留變量
- 第五章.變量調節器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.組合修改器
- 第七章.內置函數
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定義函數
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.調試控制臺
- III.模板程序員篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty類變量
- $template_dir
- 第十三章.Smarty類方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.緩存
- 建立緩存
- 多重緩存
- 緩存集合
- 控制插件輸出的可緩存性
- 第十五章.高級特性
- 安全
- 通過模板更改設置
- 模板繼承
- 數據流
- 對象
- 靜態類
- 預過濾器
- 后過濾器
- 輸出過濾器
- 緩存處理函數
- 資源
- 第十六章.以插件擴展Smarty
- 插件如何工作
- 命名約定
- 編寫插件
- 模板函數
- 調節器
- 塊函數
- 編譯函數
- 預濾器/后濾器
- 輸出過濾器
- 資源
- 插入
- Ⅳ.附錄
- 第十七章.疑難解答
- Smarty/PHP 錯誤
- 第十八章.使用技巧和經驗
- 空白變量處理
- 默認變量處理
- 傳遞變量標題給頭模板
- 日期
- WAP/WML
- 組件化模板
- 拒絕電子郵件地址
- 第十九章. 相關資源
- 第二十章. 漏洞
- 3.0安裝包
- 2.x版本升級至3.x版本的提示
- 3.0.x使用指南
- 翻譯人員列表