{append}

{append}可以在 运行时 为数组变量增加或者创建值。

Note

这需要考虑的是,对于模板中变量的赋值,最好尽量是放到PHP程序代码中进行。

属性:

参数名称 类型 必选参数 默认值 说明
var string Yes n/a 赋值的变量名称
value string Yes n/a 赋予的值
index string No n/a 数组元素的新索引,如果没有提供此参数,则附加的值会加到数组最后。
scope string No n/a 赋值变量的访问范围: 'parent','root' 或 'global'

可选的标记:

名称 说明
nocache 为变量增加'nocache'属性


Example 7.7. {append}

  1. {append var='name' value='Bob' index='first'}
  2. {append var='name' value='Meyer' index='last'}
  3. // or
  4. {append 'name' 'Bob' index='first'} {* short-hand *}
  5. {append 'name' 'Meyer' index='last'} {* short-hand *}
  6.  
  7. The first name is {$name.first}.<br>
  8. The last name is {$name.last}.
  9.  

输出:

  1. The first name is Bob.
  2. The last name is Meyer.
  3.  

参见append()getTemplateVars().

原文: https://www.smarty.net/docs/zh_CN/language.function.append.tpl