smarty section foreach遍历多维数组
说明:
section 一般针对键号是连续数字的
array(
0 => 1,
1 => 2
)
如果你的键号是字符串的,那么一定要用foreach来实现
array(
'xxx' => 1,
'yyy' => 2
)
- 多维数组section实例
例如数据集为:
Array ( [0] => Array ( [Tid] => 21 [Updated] => 2007-12-31 10:21:18 [Created] => 2007-12-31 10:21:18 [Typename] => 系统相关 [Fid] => 0 ) [1] => Array ( [Tid] => 20 [Updated] => 2007-12-31 10:21:07 [Created] => 2007-12-31 10:21:07 [Typename] => Html/Css/Ajax [Fid] => 0 [SmallType] => Array ( [0] => Array ( [Tid] => 25 [Updated] => 2007-12-31 10:25:42 [Created] => 2007-12-31 10:25:42 [Typename] => HtmlBook [Fid] => 20 [TopArticle] => Array ( [0] => Array ( [Atcid] => 9 [Title] => 文章测试 [Tid] => 25 [Created] => 2007-12-31 10:27:13 [Hits] => 1 [Atc_guest] => ) ) ) ) ) [2] => Array ( [Tid] => 17 [Updated] => 2007-12-31 10:20:34 [Created] => 2007-12-31 07:55:34 [Typename] => PHP编程 [Fid] => 0 [SmallType] => Array ( [0] => Array ( [Tid] => 24 [Updated] => 2007-12-31 10:22:16 [Created] => 2007-12-31 10:22:16 [Typename] => Fleaphp框架 [Fid] => 17 [TopArticle] => Array ( ) ) [1] => Array ( [Tid] => 23 [Updated] => 2007-12-31 10:21:50 [Created] => 2007-12-31 10:21:50 [Typename] => PHP代码库 [Fid] => 17 [TopArticle] => Array ( [0] => Array ( [Atcid] => 13 [Title] => 特殊贴 [Tid] => 23 [Created] => 2008-02-26 09:59:25 [Hits] => 1 [Atc_guest] => ) ) ) [2] => Array ( [Tid] => 22 [Updated] => 2007-12-31 10:21:33 [Created] => 2007-12-31 10:21:33 [Typename] => PHP基础 [Fid] => 17 [TopArticle] => Array ( [0] => Array ( [Atcid] => 12 [Title] => Smarty生成分页静态页面 [Tid] => 22 [Created] => 2007-12-31 10:40:08 [Hits] => 1 [Atc_guest] => ) [1] => Array ( [Atcid] => 11 [Title] => smarty生成静态页面 [Tid] => 22 [Created] => 2007-12-31 10:36:28 [Hits] => 1 [Atc_guest] => ) ) ) ) ) ) 在使用模板时:可以采用以下写法: {{section name=list loop=$Atcs}}<!-- 一级类别 --> <div align="left">{{$Atcs[list].Typename}}</div>
{{assign var=SmallType value=$Atcs[list].SmallType}}<!-- 二级类别 --> {{section name=list1 loop=$SmallType}} <div align="left"> -->{{$SmallType[list1].Typename}}</div>
{{assign var=TopAtc value=$SmallType[list1].TopArticle}}<!-- 二级分类中 的几篇文章 --> {{section name=list2 loop=$TopAtc}} <div align="left"> -------{{$TopAtc[list2].Title}} -----{{$TopAtc[list2].Created}}</div> {{/section}}
{{/section}} <hr /> {{/section}}
显示结果为:
系统相关
Html/Css/Ajax
-->HtmlBook
--------文章测试 -------2007-12-31 10:27:13
PHP编程
-->Fleaphp框架
-->PHP代码库
--------特殊贴 -------2008-02-26 09:59:25
-->PHP基础
--------Smarty生成分页静态页面 -------2007-12-31 10:40:08
--------smarty生成静态页面 -------2007-12-31 10:36:28
foreach遍历多维数组
数组的样子为:
array(
'table1' => array(
'seq' => kkk
'title' => b
)
)
{foreach key=key item=item from=$dataList name=foo} {section name=l loop=$item} <li><a href="#{$item[l].SEQ}" >·{$item[l].TITLE}</a></li> {/section} {/foreach}