Tag {foreach} ============= The tag `{foreach}` construct provides an easy way to iterate over arrays and ranges. ```smarty {foreach $list as [$key =>] $value [index=$index] [first=$first] [last=$last]} {* ...code... *} {break} {* ...code... *} {continue} {* ...code... *} {foreachelse} {* ...code... *} {/foreach} ``` ### {foreach} On each iteration, the value of the current element is assigned to `$value` and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element). ```smarty {foreach $list as $value}
{$value}
{/foreach} ``` The next form will additionally assign the current element's key to the `$key` variable on each iteration. ```smarty {foreach $list as $key => $value}
{$key}: {$value}
{/foreach} ``` Gets the current array index, starting with zero. ```smarty {foreach $list as $value}
№{$value@index}: {$value}
{/foreach} or {foreach $list as $value index=$index}
№{$index}: {$value}
{/foreach} ``` Detect first iteration: ```smarty {foreach $list as $value}
{if $value@first} first item {/if} {$value}
{/foreach} or {foreach $list as $value first=$first}
{if $first} first item {/if} {$value}
{/foreach} ``` `$first` is `TRUE` if the current `{foreach}` iteration is first iteration. Detect last iteration: ```smarty {foreach $list as $value}
{if $value@last} last item {/if} {$value}
{/foreach} or {foreach $list as $value last=$last}
{if $last} last item {/if} {$value}
{/foreach} ``` `$last` is set to `TRUE` if the current `{foreach}` iteration is last iteration. ### {break} Tag `{break}` aborts the iteration. ### {continue} Tag `{continue}` leaves the current iteration and begins with the next iteration. ### {foreachelse} `{foreachelse}` is executed when there are no values in the array variable. ```smarty {set $list = []} {foreach $list as $value}
{if $last} last item {/if} {$value}
{foreachelse}
empty
{/foreach} ``` `{foreachelse}` does not support tags `{break}` and `{continue}`.