| 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 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 常被使用在一維關聯陣列,且 foreach 的語法相較於 section 更為簡單,但是只能使用在一維陣列上。 foreach 標籤必須和 /foreach 標籤成對。需要的參數是 from 與 item。foreach 迴圈的名稱可以讓你自由選擇,可由字母、數字、或是底線組成。 foreach 迴圈可以是巢狀的,但是巢狀迴圈的名稱必須不一樣。 from 變數(通常是陣列)會決定 foreach 中執行的次數。當沒有變數值在 from 時就會執行 foreachelse。
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 迴圈也有他們處理 foreach 屬性時專用的變數,這些變數如: {$smarty.foreach.foreachname.varname},在這個變數中 foreachname 代表的是 foreach 中 name 屬性的值
iteration is used to display the current loop iteration.
iteration 會逐字顯示現在 iteration 迴圈的內容。
Iteration always starts with 1 and is incremented by one one each iteration.
Iteration 必須從1開始且每次只能增加1。
first is set to true if the current foreach iteration is the first one.
如果現在的 foreach iteration 的位置在第一個元素的話則 first 的值會是 true。
last is set to true if the current foreach iteration is the last one.
如果現在的 foreach iteration 的位置在最後一個元素的話則 last 的值會是 true 。
show is used as a parameter to foreach. show is a boolean value, true or false. If false, the foreach will not be displayed. If there is a foreachelse present, that will be alternately displayed.
show 是 foreach 的參數。 show 是個布林值,true 或 false。如果是 false 的話,則 foreach 將不會顯示值,如果用在 foreachelse 的話則會顯示值。