Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

{call}

{call}可以调用一个通过 {function} 标签来定义的模板函数,如同调用插件函数一样。

Note

模板函数是全局定义的。因为Smarty编译器是一个单次的编译器,所以 {call} 标签调用的函数,务必定义在当前模板之外的位置。 或者你可以直接通过{funcname ...}的方式来使用该函数。

  • {call}必须设置name属性, 标识要调用的模板函数的名称。

  • 可以按照属性的方式来给函数传递参数。

属性:

参数名称 类型 必选参数 默认值 说明
name string Yes n/a 模板函数的名称
assign string No n/a 将函数的返回内容赋值到指定变量
[var ...] [var type] No n/a 传递给模板函数的参数

可选的标记:

名称 说明
nocache 以不缓存的模式调用该函数

Example 7.20. 调用一个递归菜单的例子


{* define the function *}
{function name=menu level=0}
  <ul class="level{$level}">
  {foreach $data as $entry}
    {if is_array($entry)}
      <li>{$entry@key}</li>
      {call name=menu data=$entry level=$level+1}
    {else}
      <li>{$entry}</li>
    {/if}
  {/foreach}
  </ul>
{/function}

{* create an array to demonstrate *}
{$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3' =>
['item3-3-1','item3-3-2']],'item4']}

{* run the array through the function *}
{call name=menu data=$menu}
{call menu data=$menu} {* short-hand *}

  

输出:


* item1
* item2
* item3
      o item3-1
      o item3-2
      o item3-3
            + item3-3-1
            + item3-3-2
* item4

  

参见 {function}

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors