foreach,foreachelse

Attribute NameTypeRequiredDefaultDescription
fromarrayYesn/aThe array you are looping through
itemstringYesn/aThe name of the variable that is the current element
keystringNon/aThe name of the variable that is the current key
namestringNon/aThe 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 標籤成對。需要的參數是 fromitem。foreach 迴圈的名稱可以讓你自由選擇,可由字母、數字、或是底線組成。 foreach 迴圈可以是巢狀的,但是巢狀迴圈的名稱必須不一樣。 from 變數(通常是陣列)會決定 foreach 中執行的次數。當沒有變數值在 from 時就會執行 foreachelse

Example 7-4. foreach

{* this example will print out all the values of the $custid array *}
{foreach from=$custid item=curr_id}
	id: {$curr_id}<br>
{/foreach}

OUTPUT:

id: 1000<br>
id: 1001<br>
id: 1002<br>

Example 7-5. foreach key

{* The key contains the key for each looped value

assignment looks like this:

$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
      array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));

*}

{foreach name=outer item=contact from=$contacts}
  {foreach key=key item=item from=$contact}
    {$key}: {$item}<br>
  {/foreach}
{/foreach}

OUTPUT:

phone: 1<br>
fax: 2<br>
cell: 3<br>
phone: 555-4444<br>
fax: 555-3333<br>
cell: 760-1234<br>

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

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

first is set to true if the current foreach iteration is the first one.

如果現在的 foreach iteration 的位置在第一個元素的話則 first 的值會是 true。

last

last is set to true if the current foreach iteration is the last one.

如果現在的 foreach iteration 的位置在最後一個元素的話則 last 的值會是 true 。

show

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 的話則會顯示值。

total

total is used to display the number of iterations that this foreach will loop. This can be used inside or after the foreach.

total 屬性會決定此 foreach 將會遶行的次數,可以在 foreach 中或是等到 foreach 執行完後使 用。