smarty中section函数怎么取关联数组值?

2025-04-11 03:26:38
推荐回答(1个)
回答1:

1 $arr
= array(
2
array('id'=>1,'title'=>'title1'),
3
array('id'=>2,'title'=>'title2'),
4
array('id'=>3,'title'=>'title3')
5 );
6
7
$smarty->assign('news',$arr);

1 {section name=sn loop=$news}
2 {if $smarty.section.sn.first}
3


4

5

6 {/if}
7

8

9

10

11 {if $smarty.section.sn.last}
12
idtitle
{$news[sn].id}{$news[sn].title}

13 {/if}
14 {sectionelse}
15 there is no news.
16 {/section}

显示结果如下(是一个表格的样子,以下显示的没有加样式):

1 id title
2 1 title1
3 2 title2
4 3 title3