©
이 문서에서는PHP 중국어 웹사이트 매뉴얼풀어 주다
Attribute Name | Type | Required | Default | Description |
---|---|---|---|---|
from | string | Yes | n/a | The name of 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 |
属性 | 类型 | 是否必须 | 缺省值 | 描述 |
---|---|---|---|---|
from | string | Yes | n/a | 待循环数组的名称 |
item | string | Yes | n/a | 当前处理元素的变量名称 |
key | string | No | n/a | 当前处理元素的键名 |
name | string | No | n/a | 该循环的名称,用于访问该循环 |
foreachloops are an alternative tosectionloops.foreachis used to loop over a single associative array. The syntax forforeachis much easier thansection, but as a tradeoff it can only be used for a single array.foreachtags must be paired with/foreachtags. Required parameters arefromanditem. The name of the foreach loop can be anything you like, made up of letters, numbers and underscores.foreachloops can be nested, and the nested foreach names must be unique from each other. Thefromvariable (usually an array of values) determines the number of timesforeachwill loop.foreachelseis executed when there are no values in thefromvariable.
foreach是除section之外处理循环的另一种方案(根据不同需要选择不同的方案).
foreach用于处理简单数组(数组中的元素的类型一致),它的格式比section简单许多,缺点是只能处理简单数组.
foreach必须和/foreach成对使用,且必须指定from和item属性.
name属性可以任意指定(字母、数字和下划线的组合).
foreach可以嵌套,但必须保证嵌套中的foreach名称唯一.
from属性(通常是数组)决定循环的次数.
foreachelse语句在from变量没有值的时候被执行.
Example 7-4. foreach
|
Example 7-5. foreach key
|
Foreach-loops also have their own variables that handle foreach properties. These are indicated like so: {$smarty.foreach.foreachname.varname} with foreachname being the name specified as thenameattribute of foreach
foreach 循环有自己的变量名,使用该变量名可以访问该循环. 使用方法为{$smarty.foreach.foreachname.varname},其中 foreachname 即在 foreach 中指定的name属性.