?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
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 | 待循環(huán)數(shù)組的名稱 |
item | string | Yes | n/a | 當(dāng)前處理元素的變量名稱 |
key | string | No | n/a |
當(dāng)前處理元素的鍵名 |
name | string | No | n/a | 該循環(huán)的名稱,用于訪問該循環(huán) |
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 之外處理循環(huán)的另一種方案(根據(jù)不同需要選擇不同的方案).
foreach 用于處理簡單數(shù)組(數(shù)組中的元素的類型一致),它的格式比 section 簡單許多,缺點是只能處理簡單數(shù)組.
foreach 必須和 /foreach 成對使用,且必須指定
from 和
item 屬性.
name 屬性可以任意指定(字母、數(shù)字和下劃線的組合).
foreach 可以嵌套,但必須保證嵌套中的 foreach 名稱唯一.
from 屬性(通常是數(shù)組)決定循環(huán)的次數(shù).
foreachelse 語句在 from 變量沒有值的時候被執(zhí)行.
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 循環(huán)有自己的變量名,使用該變量名可以訪問該循環(huán). 使用方法為{$smarty.foreach.foreachname.varname},其中
foreachname 即在 foreach 中指定的 name 屬性.