网上的资料大多数都是用foreach,而smarty手册中,自带了一个section的例子:
- Example 7-19. sections and associative arrays
-
例 7-19. section 遍历多维数组演示
-
-
{* This is an example of printing an associative array
-
of data within a section *}
-
{section name=customer loop=$contacts}
-
name: {$contacts[customer].name}<br>
-
home: {$contacts[customer].home}<br>
-
cell: {$contacts[customer].cell}<br>
-
e-mail: {$contacts[customer].email}<p>
-
{/section}
-
-
-
OUTPUT:
-
-
name: John Smith<br>
-
home: 555-555-5555<br>
-
cell: 555-555-5555<br>
-
e-mail: john@mydomain.com<p>
-
name: Jack Jones<br>
-
home phone: 555-555-5555<br>
-
cell phone: 555-555-5555<br>
-
e-mail: jack@mydomain.com<p>
-
name: Jane Munson<br>
-
home phone: 555-555-5555<br>
-
cell phone: 555-555-5555<br>
-
e-mail: jane@mydomain.com<p>
解释:
$contacts
每次循环,都用customer来替换,而customer就是name中定义的名称,name则是customer的一个属性。
再看foreache的方案:
- <{foreach from=$myarr item=arr1 name=thearr1}>
-
2 <{foreach from=$arr1 item=arr2}>
-
4 <{foreach from=$arr2 item=single key=key}>
-
5 <{$key}>:<{$single}><br />
-
6 <{/foreach}>
-
7 <{/foreach}>
-
8 <{/foreach}>
阅读(1624) | 评论(0) | 转发(0) |