{foreach}是我们在使用smarty模板中经常使用的方法, 经常用于遍历数组或者对象数据集合并输出数据集合, 以下smarty手册是对{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循环的名称

  • from和item是必要属性
  • {foreach}循环的name可以是任何字母,数组,下划线的组合,参考PHP变量。
  • {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}循环也有自身属性的变量,可以通过{$smarty.foreach.name.property}访问,其中"name"是name属性。注意:name属性仅在需要访问{foreach}属性时有效,与{section}不同。访问未定义name的{foreach}属性不会抛出一个错误,但将导致不可预知的结果。
  • {foreach} properties are index, iteration, first, last, show, total.
  • {foreach}属性有index, iteration, first, last, show, total.

使用范例:

Example 7-5. The item attribute
例 7-5. item属性

 
  
  1. <?php 
  2. $arr = array(1000, 1001, 1002); 
  3. $smarty->assign('myArray'$arr); 
  4. ?> 

Template to output $myArray in an un-ordered list
用模板以无序列表输出$myArray

 
  
  1. <ul> 
  2. {foreach from=$myArray item=foo} 
  3.     <li>{$foo}</li> 
  4. {/foreach
  5. </ul> 

上例将输出:

 
  
  1. <ul> 
  2.     <li>1000</li> 
  3.     <li>1001</li> 
  4.     <li>1002</li> 
  5. </ul>

Example 7-6. Demonstrates the item and key attributes
例 7-6. 演示item和key属性

 
  
  1. <?php 
  2. $arr = array(9 => 'Tennis', 3 => 'Swimming', 8 => 'Coding'); 
  3. $smarty->assign('myArray'$arr); 
  4. ?> 

Template to output $myArray as key/val pair, like PHP's foreach.
用模板按键名/键值对的形式输出$myArray, 类似于PHP的foreach。

 
  
  1. <ul> 
  2. {foreach from=$myArray key=k item=v} 
  3.    <li>{$k}: {$v}</li> 
  4. {/foreach
  5. </ul> 

The above example will output:
上例将输出:

 
  
  1. <ul> 
  2.     <li>9: Tennis</li> 
  3.     <li>3: Swimming</li> 
  4.     <li>8: Coding</li> 
  5. </ul>

Example 7-7. {foreach} with associative item attribute
例 7-7. {foreach}的item属性是关联数组

 
  
  1. <?php 
  2. $items_list = array(23 => array('no' => 2456, 'label' => 'Salad'), 
  3.                     96 => array('no' => 4889, 'label' => 'Cream'
  4.                     ); 
  5. $smarty->assign('items'$items_list); 
  6. ?> 

模板中,url通过$myId输出$items

 
  
  1. <ul> 
  2. {foreach from=$items key=myId item=i} 
  3.   <li><a href="item.php?id={$myId}">{$i.no}: {$i.label}</li> 
  4. {/foreach
  5. </ul> 

上例将输出:

 
  
  1. <ul> 
  2.   <li><a href="item.php?id=23">2456: Salad</li> 
  3.   <li><a href="item.php?id=96">4889: Cream</li> 
  4. </ul> 

 


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设置一个数组,对于每个键名对应的每个循环值都包括键。

 
  
  1. <?php 
  2.  $smarty->assign('contacts'array
  3.                              array('phone' => '1'
  4.                                    'fax' => '2'
  5.                                    'cell' => '3'), 
  6.                              array('phone' => '555-4444'
  7.                                    'fax' => '555-3333'
  8.                                    'cell' => '760-1234'
  9.                              )); 
  10. ?> 


The template to output $contact.
用于输出$contact的模板。

 
  
  1. {foreach name=outer item=contact from=$contacts
  2.   <hr /> 
  3.   {foreach key=key item=item from=$contact
  4.     {$key}: {$item}<br /> 
  5.   {/foreach
  6. {/foreach

上例将输出:

 
  
  1. <hr /> 
  2.   phone: 1<br /> 
  3.   fax: 2<br /> 
  4.   cell: 3<br /> 
  5. <hr /> 
  6.   phone: 555-4444<br /> 
  7.   fax: 555-3333<br /> 
  8.   cell: 760-1234<br /> 

 


Example 7-9. Database example with {foreachelse}
例 7-9. 使用{foreachelse}的数据库示例
一个数据库(例如PEAR或ADODB)的搜索脚本示例,

 
  
  1. <?php 
  2.   $search_condition = "where name like '$foo%' "
  3.   $sql = 'select contact_id, name, nick from contacts '.$search_condition.' order by name'
  4.   $smarty->assign('results'$db->getAssoc($sql) ); 
  5. ?> 

借助{foreachelse}标记在没有结果时模板输出"None found"字样。

 
  
  1. {foreach key=cid item=con from=$results
  2.     <a href="contact.php?contact_id={$cid}">{$con.name} - {$con.nick}</a><br /> 
  3. {foreachelse} 
  4.     No items were found in the search 
  5. {/foreach

.index
.index包含当前数组索引,从零开始。
Example 7-10. index example
例 7-10. index示例

 
  
  1. {* The header block is output every five rows *} 
  2. {* 每五行输出一次头部区块 *} 
  3. <table> 
  4. {foreach from=$items key=myId item=i name=foo} 
  5.   {if $smarty.foreach.foo.index % 5 == 0} 
  6.      <tr><th>Title</th></tr> 
  7.   {/if
  8.   <tr><td>{$i.label}</td></tr> 
  9. {/foreach
  10. </table> 

 


.iteration
iteration包含当前循环次数,与index不同,从1开始,每次循环增长1。
Example 7-11. iteration and index example
例 7-11. iteration和index示例

 
  
  1. {* this will output 0|1, 1|2, 2|3, ... etc *} 
  2. {* 该例将输出0|1, 1|2, 2|3, ... 等等 *} 
  3. {foreach from=$myArray item=i name=foo} 
  4. {$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration}, 
  5. {/foreach

 


.first
first在当前{foreach}循环处于初始位置时值为TRUE。
Example 7-12. first property example
例 7-12. first属性示例

 
  
  1. {* show LATEST on the first item, otherwise the id *} 
  2. {* 对于第一个条目显示LATEST而不是id *} 
  3. <table> 
  4. {foreach from=$items key=myId item=i name=foo} 
  5. <tr> 
  6.   <td>{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}</td> 
  7.   <td>{$i.label}</td> 
  8. </tr> 
  9. {/foreach
  10. </table> 

 


.last
last在当前{foreach}循环处于最终位置是值为TRUE。
Example 7-13. last property example
例 7-13. last属性示例

 
  
  1. {* Add horizontal rule at end of list *} 
  2. {* 在列表结束时增加一个水平标记 *}) 
  3. {foreach from=$items key=part_id item=prod name=products} 
  4.   <a href="#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if
  5. {foreachelse} 
  6.   ... content ... 
  7. {/foreach


.show
show是{foreach}的参数. show是一个布尔值。如果值为FALSE,{foreach}将不被显示。如果有对应的{foreachelse},将被显示。