fenom/docs/en/tags/foreach.md

104 lines
2.1 KiB
Markdown
Raw Permalink Normal View History

2016-04-12 12:28:57 +03:00
Tag {foreach}
=============
The tag `{foreach}` construct provides an easy way to iterate over arrays and ranges.
2013-02-07 20:04:00 +04:00
2013-02-11 17:23:22 +04:00
```smarty
{foreach $list as [$key =>] $value [index=$index] [first=$first] [last=$last]}
{* ...code... *}
{break}
{* ...code... *}
{continue}
{* ...code... *}
{foreachelse}
{* ...code... *}
{/foreach}
```
2013-02-07 20:04:00 +04:00
### {foreach}
2016-04-12 12:28:57 +03:00
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).
2013-02-07 20:04:00 +04:00
2013-02-11 17:23:22 +04:00
```smarty
2013-02-07 20:04:00 +04:00
{foreach $list as $value}
<div>{$value}</div>
{/foreach}
```
2016-04-12 12:28:57 +03:00
The next form will additionally assign the current element's key to the `$key` variable on each iteration.
2013-02-07 20:04:00 +04:00
```smarty
{foreach $list as $key => $value}
<div>{$key}: {$value}</div>
{/foreach}
```
2016-04-12 12:28:57 +03:00
Gets the current array index, starting with zero.
2013-02-07 20:04:00 +04:00
```smarty
2016-05-08 00:33:23 +03:00
{foreach $list as $value}
<div>№{$value@index}: {$value}</div>
{/foreach}
or
2013-02-07 20:04:00 +04:00
{foreach $list as $value index=$index}
<div>№{$index}: {$value}</div>
{/foreach}
```
2016-04-12 12:28:57 +03:00
Detect first iteration:
2013-02-07 20:04:00 +04:00
```smarty
2016-05-08 00:33:23 +03:00
{foreach $list as $value}
<div>{if $value@first} first item {/if} {$value}</div>
{/foreach}
or
2013-02-07 20:04:00 +04:00
{foreach $list as $value first=$first}
<div>{if $first} first item {/if} {$value}</div>
{/foreach}
```
2016-05-08 00:33:23 +03:00
`$first` is `TRUE` if the current `{foreach}` iteration is first iteration.
2016-04-12 12:28:57 +03:00
Detect last iteration:
2013-02-07 20:04:00 +04:00
```smarty
2016-05-08 00:33:23 +03:00
{foreach $list as $value}
<div>{if $value@last} last item {/if} {$value}</div>
{/foreach}
or
2013-02-07 20:04:00 +04:00
{foreach $list as $value last=$last}
<div>{if $last} last item {/if} {$value}</div>
{/foreach}
```
2016-05-08 00:33:23 +03:00
`$last` is set to `TRUE` if the current `{foreach}` iteration is last iteration.
2013-02-07 20:04:00 +04:00
### {break}
2016-04-12 12:28:57 +03:00
Tag `{break}` aborts the iteration.
2013-02-07 20:04:00 +04:00
### {continue}
2016-04-12 12:28:57 +03:00
Tag `{continue}` leaves the current iteration and begins with the next iteration.
2013-02-07 20:04:00 +04:00
### {foreachelse}
2016-04-12 12:28:57 +03:00
`{foreachelse}` is executed when there are no values in the array variable.
2013-02-07 20:04:00 +04:00
```smarty
2016-04-12 12:28:57 +03:00
{set $list = []}
2013-02-07 20:04:00 +04:00
{foreach $list as $value}
<div>{if $last} last item {/if} {$value}</div>
{foreachelse}
<div>empty</div>
{/foreach}
```
2016-04-12 12:28:57 +03:00
`{foreachelse}` does not support tags `{break}` and `{continue}`.