标签:
{foreach} is used to loop over an associative array as well a numerically-indexed array, unlike {section} which is for looping over numerically-indexed arrays only. The syntax for {foreach} is much easier than {section}, but as a tradeoff it can only be used for a single array. Every {foreach} tag must be paired with a closing {/foreach} tag.
{foreach} 用于像循环访问一个数字索引数组一样循环访问一个关联数组,与仅能访问数字索引数组的{section}不同,{foreach}的语法比 {section}的语法简单得多,但是作为一个折衷方案也仅能用于单个数组。每个{foreach}标记必须与关闭标记{/foreach}成对出现。
Attribute Name 属性名称 | Type 类型 | Required 必要 | Default 默认值 | Description 描述 |
---|---|---|---|---|
from | array | Yes必要 | n/a | The array you are looping through 循环访问的数组 |
item | string | Yes必要 | n/a | The name of the variable that is the current element 当前元素的变量名 |
key | string | No可选 | n/a | The name of the variable that is the current key 当前键名的变量名 |
name | string | No可选 | n/a | The name of the foreach loop for accessing foreach properties 用于访问foreach属性的foreach循环的名称 |
Required attributes are from and item.
The name of the {foreach} loop can be anything you like, made up of letters, numbers and underscores, like PHP variables.
{foreach} loops can be nested, and the nested {foreach} names must be unique from each other.
The from attribute, usually an array of values, determines the number of times {foreach} will loop.
{foreachelse} is executed when there are no values in the from variable.
{foreach} loops also have their own variables that handle properties. These are accessed with: {$smarty.foreach.name.property} with "name" being the nameattribute.
{foreach}循环也有自身属性的变量,可以通过{$smarty.foreach.name.property}访问,其中"name"是name属性。
Note: The name attribute is only required when you want to access a {foreach} property, unlike {section}. Accessing a {foreach} property with nameundefined does not throw an error, but leads to unpredictable results instead.
注意:name属性仅在需要访问{foreach}属性时有效,与{section}不同。访问未定义name的{foreach}属性不会抛出一个错误,但将导致不可预知的结果。
{foreach} properties are index, iteration, first, last, show, total.
.index
index contains the current array index, starting with zero.
.index包含当前数组索引,从零开始。
iteration contains the current loop iteration and always starts at one, unlike index. It is incremented by one on each iteration.
iteration包含当前循环次数,与index不同,从1开始,每次循环增长1。
first is TRUE if the current {foreach} iteration is the initial one.
first在当前{foreach}循环处于初始位置时值为TRUE。
last is set to TRUE if the current {foreach} iteration is the final one.
last在当前{foreach}循环处于最终位置是值为TRUE。
show is used as a parameter to {foreach}. show is a boolean value. If FALSE, the {foreach} will not be displayed. If there is a {foreachelse} present, that will be alternately displayed.
show是{foreach}的参数. show是一个布尔值。如果值为FALSE,{foreach}将不被显示。如果有对应的{foreachelse},将被显示。
total contains the number of iterations that this {foreach} will loop. This can be used inside or after the {foreach}.
total包括{foreach}将循环的次数,既可以在{foreach}中使用,也可以在之后使用。
See also {section} and $smarty.foreach.
标签:
原文地址:http://www.cnblogs.com/hanqishihu/p/5700780.html