smary模板中多维数组的引用

1672阅读 0评论2011-07-22 lsstarboy
分类:Python/Ruby

网上的资料大多数都是用foreach,而smarty手册中,自带了一个section的例子:

  1. Example 7-19. sections and associative arrays
  2. 例 7-19. section 遍历多维数组演示

  3. {* This is an example of printing an associative array
  4.  of data within a section *}
  5. {section name=customer loop=$contacts}
  6.     name: {$contacts[customer].name}<br>
  7.     home: {$contacts[customer].home}<br>
  8.     cell: {$contacts[customer].cell}<br>
  9.     e-mail: {$contacts[customer].email}<p>
  10. {/section}


  11. OUTPUT:

  12. name: John Smith<br>
  13. home: 555-555-5555<br>
  14. cell: 555-555-5555<br>
  15. e-mail: john@mydomain.com<p>
  16. name: Jack Jones<br>
  17. home phone: 555-555-5555<br>
  18. cell phone: 555-555-5555<br>
  19. e-mail: jack@mydomain.com<p>
  20. name: Jane Munson<br>
  21. home phone: 555-555-5555<br>
  22. cell phone: 555-555-5555<br>
  23. e-mail: jane@mydomain.com<p>
解释:
  $contacts每次循环,都用customer来替换,而customer就是name中定义的名称,name则是customer的一个属性。

再看foreache的方案:

  1. <{foreach from=$myarr item=arr1 name=thearr1}>
  2. 2    <{foreach from=$arr1 item=arr2}>
  3. 4     <{foreach from=$arr2 item=single key=key}>
  4. 5     &nbsp;<{$key}><{$single}><br />
  5. 6     <{/foreach}>
  6. 7     <{/foreach}>
  7. 8    <{/foreach}>






上一篇:gprolog使用示例
下一篇:PHP中 HTTP_HOST 和 SERVER_NAME 的区别[转载]