| 屬性 | 類型 | 說明 |
|---|---|---|
| index | int | 當前迭代索引 (從 0 開始) |
| iteration | int | 當前迭代次數 (從 1 開始) |
| first | bool | 是否為第一次迭代 |
| last | bool | 是否為最後一次迭代 |
| show | bool | 是否顯示內容 (受 `foreachelse` 影響) |
| total | int | 總迭代次數 (僅當陣列已知大小時) |
{foreach $items as $item name=foo}
{$item@index} {* 索引 *}
{$item@iteration} {* 次數 *}
{$item@first} {* 首項 *}
{$item@last} {* 尾項 *}
{$item@total} {* 總數 *}
{/foreach}
{* 等價長寫法 *}
{foreach from=$items item=item name=foo}
...
{/foreach}
{foreach $VIDEO_INFO_LIST as $video name=vlist}
{if $smarty.foreach.vlist.iteration <= 5}
<li>{$video.title}</li>
{/if}
{/foreach}
{* 或使用 index (從 0 開始) *}
{foreach $VIDEO_INFO_LIST as $video name=vlist}
{if $smarty.foreach.vlist.index < 5}
<li>{$video.title}</li>
{/if}
{/foreach}
{$VIDEO_INFO_LIST|@count}
{* 或在 foreach 內 *}
{$smarty.foreach.vlist.total}
{foreach $categories as $cat name=cat}
{$smarty.foreach.cat.iteration}. {$cat.name}
{foreach $cat.posts as $post name=post}
- {$post.title} (外層索引: {$smarty.foreach.cat.index})
{/foreach}
{/foreach}
{foreach $emptyArray as $item}
{$item}
{foreachelse}
暫無資料
{/foreach}