{foreach}循環也有自身屬性的變量,可以通過{$smarty.foreach.name.property}通路,其中"name"是name屬性。
Note: The name attribute is only required when you want to access a {foreach} property, unlike {section}. Accessing a {foreach} property with name undefined does not throw an error, but leads to unpredictable results instead.
注意:name屬性僅在需要通路{foreach}屬性時有效,與{section}不同。通路未定義name的{foreach}屬性不會抛出一個錯誤,但将導緻不可預知的結果。
{foreach} properties are index, iteration, first, last, show, total.
Example 7-5. The item attribute
例 7-5. item屬性
<code><?php $arr = array(1000, 1001, 1002); $smarty->assign('myArray', $arr); ?></code>
Template to output $myArray in an un-ordered list
用模闆以無序清單輸出$myArray
The above example will output:
上例将輸出:
Example 7-6. Demonstrates the item and key attributes
例 7-6. 示範item和key屬性
<code><?php $arr = array(9 => 'Tennis', 3 => 'Swimming', 8 => 'Coding'); $smarty->assign('myArray', $arr); ?></code>
Template to output $myArray as key/val pair, like PHP's foreach.
用模闆按鍵名/鍵值對的形式輸出$myArray, 類似于PHP的foreach。
Example 7-7. {foreach} with associative item attribute
例 7-7. {foreach}的item屬性是關聯數組
<code><?php $items_list = array(23 => array('no' => 2456, 'label' => 'Salad'), 96 => array('no' => 4889, 'label' => 'Cream') ); $smarty->assign('items', $items_list); ?></code>
Template to output $items with $myId in the url
模闆中,url通過$myId輸出$items
Example 7-8. {foreach} with nested item and key
例 7-8. {foreach}使用嵌套的item和key
Assign an array to Smarty, the key contains the key for each looped value.
向Smarty設定一個數組,對于每個鍵名對應的每個循環值都包括鍵。
<code><?php $smarty->assign('contacts', array( array('phone' => '1', 'fax' => '2', 'cell' => '3'), array('phone' => '555-4444', 'fax' => '555-3333', 'cell' => '760-1234') )); ?></code>
The template to output $contact.
用于輸出$contact的模闆。
Example 7-9. Database example with {foreachelse}
例 7-9. 使用{foreachelse}的資料庫示例
A database (eg PEAR or ADODB) example of a search script, the query results assigned to Smarty
一個資料庫(例如PEAR或ADODB)的搜尋腳本示例,
<code><?php $search_condition = "where name like '$foo%' "; $sql = 'select contact_id, name, nick from contacts '.$search_condition.' order by name'; $smarty->assign('results', $db->getAssoc($sql) ); ?></code>
The template which display "None found" if no results with {foreachelse}.
借助{foreachelse}标記在沒有結果時模闆輸出"None found"字樣。
index contains the current array index, starting with zero.
.index包含目前數組索引,從零開始。
Example 7-10. index example
例 7-10. index示例
<code>{* The header block is output every five rows *} {* 每五行輸出一次頭部區塊 *} <table> {foreach from=$items key=myId item=i name=foo} {if $smarty.foreach.foo.index % 5 == 0} <tr><th>Title</th></tr> {/if} <tr><td>{$i.label}</td></tr> {/foreach} </table></code>
iteration contains the current loop iteration and always starts at one, unlike index. It is incremented by one on each iteration.
iteration包含目前循環次數,與index不同,從1開始,每次循環增長1。
Example 7-11. iteration and index example
例 7-11. iteration和index示例
<code>{* this will output 0|1, 1|2, 2|3, ... etc *} {* 該例将輸出0|1, 1|2, 2|3, ... 等等 *} {foreach from=$myArray item=i name=foo} {$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration}, {/foreach}</code>
first is TRUE if the current {foreach} iteration is the initial one.
first在目前{foreach}循環處于初始位置時值為TRUE。
Example 7-12. first property example
例 7-12. first屬性示例
<code>{* show LATEST on the first item, otherwise the id *} {* 對于第一個條目顯示LATEST而不是id *} <table> {foreach from=$items key=myId item=i name=foo} <tr> <td>{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}</td> <td>{$i.label}</td> </tr> {/foreach} </table></code>
last is set to TRUE if the current {foreach} iteration is the final one.
last在目前{foreach}循環處于最終位置是值為TRUE。
Example 7-13. last property example
例 7-13. last屬性示例
<code>{* Add horizontal rule at end of list *} {* 在清單結束時增加一個水準标記 *}) {foreach from=$items key=part_id item=prod name=products} <a href="#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if} {foreachelse} ... content ... {/foreach}</code>
show is used as a parameter to {foreach}. show is a boolean value. If FALSE, the {foreach} will not be displayed. If there is a {foreachelse} present, that will be alternately displayed.
show是{foreach}的參數. show是一個布爾值。如果值為FALSE,{foreach}将不被顯示。如果有對應的{foreachelse},将被顯示。
total contains the number of iterations that this {foreach} will loop. This can be used inside or after the {foreach}.
total包括{foreach}将循環的次數,既可以在{foreach}中使用,也可以在之後使用。
Example 7-14. total property example
例 7-14. total屬性示例
<code>{* show rows returned at end *} {* 在結束位置顯示行數 *} {foreach from=$items key=part_id item=prod name=foo} {$prod.name><hr/> {if $smarty.foreach.foo.last} <div id="total">{$smarty.foreach.foo.total} items</div> {/if} {foreachelse} ... something else ... {/foreach}</code>