<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.foreach.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.iflese.html" accesskey="N">Next</a></td></tr></table>
# {function} 函數
{function} is used to create functions within a template and call them just like a plugin function.
Instead of writing a plugin that generates presentational content, keeping it in the template is often a more manageable choice. It also simplifies data traversal, such as deeply nested menus.
<table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/>Template functions are defined global. Since the Smarty compiler is a single-pass compiler, The {call} tag must be used to call a template function defined externally from the given template.<br/>模板函數具全局作用域,因為Smarty編譯器是單進程的編譯器,<a href="language.function.call.html">{call}</a>標簽必須用來調用給定模板的外部定義模板函數。</td> </tr></table>
Otherwise you can directly use the function as {funcname ...} in the template.
* The {function} tag must have the name attribute which contains the the name of the template function. A tag with this name can be used to call the template function.
* Default values for variables can be passed to the template function as attributes. The default values can be overwritten when the template function is being called..
* You can use all variables from the calling template inside the template function. Changes to variables or new created variables inside the template function have local scope and are not visible inside the calling template after the template function is executed.
{function}用來在模板中創建函數,可以像調用插件函數一樣調用它們。
取代在插件中寫表象內容的函數,讓模板保持一致性通常是個更好的選擇。它也簡化了對數據的遍歷,例如深度的嵌套菜單。
另外你可以在模板中直接使用{funcname...}函數。
{function}標簽必須包含模板函數名的name屬性,該name標簽名必須能夠調用模板函數;
默認變量值應能作為屬性傳遞到模板函數,當模板函數被調用的時候,默認值應能被復寫;
在模板函數內部應能使用被調用模板的所有變量值,在模板函數中更改或新建變量的值必須具局部作用域,而且在執行模板函數后這些變量值在被調用模板內部應不可見。
**Attribute: **
| Attribute Name | Type | Required | Default | Description |
|-----|-----|-----|-----|-----|
| name | string | yes | *n/a* | The name of the template function |
| [var ...] | [var type] | no | n/a | default variable value to pass local to the template function |
**屬性: **
| 屬性名 | 類型 | 是否必須 | 缺省值 | 描述 |
|-----|-----|-----|-----|-----|
| name | int | yes | *n/a* | 模板函數名 |
| [var...] | [var type] | no | n/a | 由本地傳遞到模板函數的變量值 |
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN741" id="AEN741"> </a> <b>Example 7.41. Recursive menu {function} example<br/> 例 7-41.遞歸菜單的{function}</b><strong>例子</strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> {* define the function *}<br/> {function name=menu level=0}<br/> {function menu level=0} {* short-hand *}<br/> <ul class="level{$level}"><br/> {foreach $data as $entry}<br/> {if is_array($entry)}<br/> <li>{$entry@key}</li><br/> {menu data=$entry level=$level+1}<br/> {else}<br/> <li>{$entry}</li><br/> {/if}<br/> {/foreach}<br/> </ul><br/> {/function}</p> <p><br/> {* create an array to demonstrate *}<br/> {$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3' => ['item3-3-1','item3-3-2']],'item4']}<br/> {* run the array through the function *}<br/> {menu data=$menu}</p> <p>Will generate the following output<br/> - item1<br/> - item2<br/> - item3<br/> -- item3-1<br/> -- item3-2<br/> -- item3-3<br/> --- item3-3-1<br/> --- item3-3-2<br/> - item4</p> </td> </tr></table><p> 參考<a href="language.function.call.html">{call}</a>。<br/></p> </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.foreach.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.iflese.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">{foreach},{foreachelse}<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">{if},{elseif},{else}<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使用指南
- 翻譯人員列表