©
本文档使用
php.cn手册 发布
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 | 该循环的名称,用于访问该循环 |
foreach loops are an alternative to section loops. foreach is used to loop over a single associative array. The syntax for foreach is much easier than section , but as a tradeoff it can only be used for a single array. foreach tags must be paired with /foreach tags. Required parameters are from and item . The name of the foreach loop can be anything you like, made up of letters, numbers and underscores. foreach loops can be nested, and the nested foreach names must be unique from each other. The from variable (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 是除 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 the
name
attribute of foreach
foreach 循环有自己的变量名,使用该变量名可以访问该循环. 使用方法为{$smarty.foreach.foreachname.varname},其中
foreachname 即在 foreach 中指定的 name 属性.