您的位置:首页 > 其它

smarty模板section,foreach循环用法

2016-05-04 00:41 295 查看
{foreach name=foreach_name key=k item=v from=$arr}

{$k}=>{$v}<br/>

{/foreach}

{section name=sec_name loop=$arr start=0 step=n max= m show=true/false }

{if is_array($arr[sec_name])}

{section name=sec2 loop=$arr[sec_name]}

{$arr[sec_name][sec2]}<br/>

{/section}

{esle}

{$arr[sec_name]}<br/>

{/if}

{$smarty.section.sec_name.index}{*当前循环下标,默认为0,例如0,1,2,3...*}

{$smarty.section.sec_name.index_prev}{*当前循环下标的前一个值,index为0时,index_prev为-1*}

{$smarty.section.sec_name.index_next}{*后一个值,默认为1*}

{$smarty.section.sec_name.first}{*是否为第一个循环,是为true*}

{$smarty.section.sec_name.last}{*是否为最后一个循环*}

{$smarty.section.sec_name.iteration}{*当前行号,如1,2,3,4...*}rownum是iteration的别名

{$smarty.section.sec_name.loop}{*最后一个循环号,数组元素为7个,实际循环4次(例:max=4),loop仍然显示为7*}

{$smarty.section.sec_name.total}{*实际循环次数*}

max规定最大循环次数;

show是否显示这部分内容;决定是否对这块进行显示

{/section}

section在 循环一维带键值的数组时,无法显示数组内容:

例如:$arr=array('one'=>'bo','two'=>"yuan",'three'=>'mm','four'=>'bb');

{section name=n loop=$arr}

元素:{$arr
}

{/section}

结果则是:

section循环

元素:
元素:
元素:
元素:无内容

如果把数组arr的元素换成‘two’=>array()等,循环到元素two时同样无法得到结果。

$arr=array('object'=>'book','type'=>'computer');

smarty模板文件中对数组的读取:1.通过索引$arr[0];2.通过键值,$arr.type
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: