index包含当前数组索引,从零开始。
index示例
{* The header block is output every five rows *}
{* 每五行输出一次头部区块 *}
-
<table>
-
{foreach from=$items key=myId item=i name=foo}
-
{if $smarty.foreach.foo.index % 5 == 0}
-
<tr><th>Title</th></tr>
-
{/if}
-
<tr><td>{$i.label}</td></tr>
-
{/foreach}
-
</table>
.iteration包含当前循环次数,与index不同,从1开始,每次循环增长1。
iteration和index示例
{* this will output 0|1, 1|2, 2|3, ... etc *}
{* 该例将输出0|1, 1|2, 2|3, ... 等等 *}
-
{foreach from=$myArray item=i name=foo}
-
{$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
-
{/foreach}
.first:first为TRUE,如果当前的{foreach}遍历是第一个。
{* 第一个项目显示LATEST,其它只是显示ID *}
{foreach from=$items key=myId item=i name=foo}
{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if} |
{$i.label} |
{/foreach}
.last:last为TRUE,如果当前的{foreach}遍历是最后一个。
{* 在列表的最后加水平分割 *}
-
{foreach from=$items key=part_id item=prod name=products}
-
<a href=\"#{$part_id}\">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if}
-
{foreachelse}
-
... content ...
-
{/foreach}
.show
show是{foreach}的一个参数。show是个布尔值。如果为FALSE,{foreach}将不会显示。而如果这时有{foreachelse}的存在,将改而显示它的内容。
.total
total包含了该{foreach}可以循环的次数。可以用在{foreach}之内或之外。
{* 在最后显示返回的行数 *}
-
{foreach from=$items key=part_id item=prod name=foo}
-
{$prod.name><hr/>
-
{if $smarty.foreach.foo.last}
-
<div id=\"total\">{$smarty.foreach.foo.total} items</div>
-
{/if}
-
{foreachelse}
-
... something else ...
-
{/foreach}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29753604/viewspace-1249320/
,如需转载,请注明出处,否则将追究法律责任。