🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
<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/> &lt;ul class="level{$level}"&gt;<br/> {foreach $data as $entry}<br/> {if is_array($entry)}<br/> &lt;li&gt;{$entry@key}&lt;/li&gt;<br/> {menu data=$entry level=$level+1}<br/> {else}<br/> &lt;li&gt;{$entry}&lt;/li&gt;<br/> {/if}<br/> {/foreach}<br/> &lt;/ul&gt;<br/> {/function}</p> <p><br/> {* create an array to demonstrate *}<br/> {$menu = ['item1','item2','item3' =&gt; ['item3-1','item3-2','item3-3' =&gt; ['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>