smarty -- foreach用法


 {foreach},{foreachelse}

用於像訪問序數數組一樣訪問關聯數組

{foreach},{foreachelse}

{foreach} is used to loop over an associative array as well a numerically-indexed array, unlike {section} which is for looping over numerically-indexed arrays only. The syntax for {foreach} is much easier than {section}, but as a tradeoff it can only be used for a single array. Every {foreach} tag must be paired with a closing {/foreach} tag.

{foreach} 用於像循環訪問一個數字索引數組一樣循環訪問一個關聯數組,與僅能訪問數字索引數組的{section}不同,{foreach}的語法比 {section}的語法簡單得多,但是作為一個折衷方案也僅能用於單個數組。每個{foreach}標記必須與關閉標記{/foreach}成對出現。

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屬性的foreach循環的名稱
  • Required attributes are from and item.

  • from和item是必要屬性
  • The name of the {foreach} loop can be anything you like, made up of letters, numbers and underscores, like PHP variables.

  • {foreach}循環的name可以是任何字母,數組,下划線的組合,參考PHP變量。
  • {foreach} loops can be nested, and the nested {foreach} names must be unique from each other.

  • {foreach}循環可以嵌套,嵌套的{foreach}的名稱應當互不相同。
  • The from attribute, usually an array of values, determines the number of times {foreach} will loop.

  • from屬性通常是值數組,被用於判斷{foreach}的循環次數。
  • {foreachelse} is executed when there are no values in the from variable.

  • 在from變量中沒有值時,將執行{foreachelse}。
  • {foreach} loops also have their own variables that handle properties. These are accessed with: {$smarty.foreach.name.property} with "name" being the nameattribute.

  • {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 nameundefined does not throw an error, but leads to unpredictable results instead.

  • 注意:name屬性僅在需要訪問{foreach}屬性時有效,與{section}不同。訪問未定義name的{foreach}屬性不會拋出一個錯誤,但將導致不可預知的結果。

  • {foreach} properties are indexiterationfirstlastshowtotal.

  • {foreach}屬性有index, iteration, first, last, show, total.   
  1. Example 7-7. {foreach} with associative item attribute
  2.  
  3. 7-7. {foreach}的item屬性是關聯數組
  4.  
  5. <?php
  6. $items_list = array(
  7. 23 => array('no' => 2456, 'label' => 'Salad'),
  8. 96 => array('no' => 4889, 'label' => 'Cream')
  9. );
  10. $smarty->assign('items', $items_list);
  11. ?>
  12.  
  13. Template to output $items with $myId in the url
  14.  
  15. 模板中,url通過$myId輸出$items
  16.  
  17. <ul>
  18. {foreach from=$items key=myId item=i}
  19. <li><a href="http://blog.163.com/lgh_2002/blog/item.php?id={$myId}">{$i.no}: {$i.label}</li>
  20. {/foreach}
  21. </ul>
  22.  
  23. The above example will output:
  24.  
  25. 上例將輸出:
  26.  
  27. <ul>
  28. <li><a href="http://blog.163.com/lgh_2002/blog/item.php?id=23">2456: Salad</li>
  29. <li><a href="http://blog.163.com/lgh_2002/blog/item.php?id=96">4889: Cream</li>
  30. </ul>
  1. Example 7-8. {foreach} with nested item and key
  2.  
  3. 7-8. {foreach}使用嵌套的itemkey
  4.  
  5. Assign an array to Smarty, the key contains the key for each looped value.
  6.  
  7. Smarty設置一個數組,對於每個鍵名對應的每個循環值都包括鍵。
  8.  
  9. <?php
  10. $smarty->assign('contacts', array(
  11. array('phone' => '1',
  12. 'fax' => '2',
  13. 'cell' => '3'),
  14. array('phone' => '555-4444',
  15. 'fax' => '555-3333',
  16. 'cell' => '760-1234')
  17. ));
  18. ?>
  19.  
  20. The template to output $contact.
  21.  
  22. 用於輸出$contact的模板。
  23.  
  24. <foreach name=outer item=contact from=$contacts}
  25. <hr />
  26. {foreach key=key item=item from=$contact}
  27. {$key}: {$item}<br />
  28. {/foreach}
  29. {/foreach}
  30.  
  31. The above example will output:
  32.  
  33. 上例將輸出:
  34.  
  35. <hr />
  36. phone: 1<br />
  37. fax: 2<br />
  38. cell: 3<br />
  39. <hr />
  40. phone: 555-4444<br />
  41. fax: 555-3333<br />
  42. cell: 760-1234<br />


.index

index contains the current array index, starting with zero.

.index包含當前數組索引,從零開始。

 

  1. {* 每五行輸出一次頭部區塊 *}
  2. <table>
  3. {foreach from=$items key=myId item=i name=foo}
  4. {if $smarty.foreach.foo.index % 5 == 0}
  5. <tr><th>Title</th></tr>
  6. {/if}
  7. <tr><td>{$i.label}</td></tr>
  8. {/foreach}
  9. </table>      

 

.iteration

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。

  1. {* 該例將輸出0|1, 1|2, 2|3, ... 等等 *}
  2. {foreach from=$myArray item=i name=foo}
  3. {$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
  4. {/foreach}   

.first

first is TRUE if the current {foreach} iteration is the initial one.

first在當前{foreach}循環處於初始位置時值為TRUE。

  1. {* 對於第一個條目顯示LATEST而不是id *}
  2. <table>
  3. {foreach from=$items key=myId item=i name=foo}
  4. <tr>
  5. <td>{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}</td>
  6. <td>{$i.label}</td>
  7. </tr>
  8. {/foreach}
  9. </table>   

.last

last is set to TRUE if the current {foreach} iteration is the final one.

last在當前{foreach}循環處於最終位置是值為TRUE。

  1. {* 在列表結束時增加一個水平標記 *})
  2. {foreach from=$items key=part_id item=prod name=products}
  3. <a href="http://blog.163.com/lgh_2002/blog/#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if}
  4. {foreachelse}
  5. ... content ...
  6. {/foreach}  

.show

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

total contains the number of iterations that this {foreach} will loop. This can be used inside or after the {foreach}.

total包括{foreach}將循環的次數,既可以在{foreach}中使用,也可以在之后使用。

  1. {* 在結束位置顯示行數 *}
  2. {foreach from=$items key=part_id item=prod name=foo}
  3. {$prod.name><hr/>
  4. {if $smarty.foreach.foo.last}
  5. <div id="total">{$smarty.foreach.foo.total} items</div>
  6. {/if}
  7. {foreachelse}
  8. ... something else ...
  9. {/foreach}     

See also {section} and $smarty.foreach.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM