This commit is contained in:
bzick 2016-05-08 00:33:23 +03:00
parent ba4ba548ff
commit e289b9b3b1
2 changed files with 39 additions and 8 deletions

View File

@ -37,6 +37,12 @@ The next form will additionally assign the current element's key to the `$key` v
Gets the current array index, starting with zero. Gets the current array index, starting with zero.
```smarty ```smarty
{foreach $list as $value}
<div>№{$value@index}: {$value}</div>
{/foreach}
or
{foreach $list as $value index=$index} {foreach $list as $value index=$index}
<div>№{$index}: {$value}</div> <div>№{$index}: {$value}</div>
{/foreach} {/foreach}
@ -45,22 +51,34 @@ Gets the current array index, starting with zero.
Detect first iteration: Detect first iteration:
```smarty ```smarty
{foreach $list as $value}
<div>{if $value@first} first item {/if} {$value}</div>
{/foreach}
or
{foreach $list as $value first=$first} {foreach $list as $value first=$first}
<div>{if $first} first item {/if} {$value}</div> <div>{if $first} first item {/if} {$value}</div>
{/foreach} {/foreach}
``` ```
`$first` is `TRUE` if the current `{foreach}` iteration is the initial one. `$first` is `TRUE` if the current `{foreach}` iteration is first iteration.
Detect last iteration: Detect last iteration:
```smarty ```smarty
{foreach $list as $value}
<div>{if $value@last} last item {/if} {$value}</div>
{/foreach}
or
{foreach $list as $value last=$last} {foreach $list as $value last=$last}
<div>{if $last} last item {/if} {$value}</div> <div>{if $last} last item {/if} {$value}</div>
{/foreach} {/foreach}
``` ```
`$last` is set to `TRUE` if the current `{foreach}` iteration is the final one. `$last` is set to `TRUE` if the current `{foreach}` iteration is last iteration.
### {break} ### {break}

View File

@ -38,11 +38,12 @@
{/foreach} {/foreach}
``` ```
Получение номера (индекса) итерации, начиная с 0 Получение номера (индекса) итерации, начиная с 0
```smarty ```smarty
{foreach $list as $value} {foreach $list as $value}
<div>№{$value:index}: {$value}</div> <div>№{$value@index}: {$value}</div>
{/foreach} {/foreach}
или или
@ -52,20 +53,32 @@
{/foreach} {/foreach}
``` ```
Определение первого элемента Определение первой итерации:
```smarty ```smarty
{foreach $list as $value} {foreach $list as $value}
<div>{if $value:first} first item {/if} {$value}</div> <div>{if $value@first} first item {/if} {$value}</div>
{/foreach}
или
{foreach $list as $value first=$first}
<div>{if $first} first item {/if} {$value}</div>
{/foreach} {/foreach}
``` ```
Переменная `$value:first` будет иметь значение **TRUE**, если текущая итерация является первой. Переменная `$value@first` будет иметь значение **TRUE**, если текущая итерация является первой.
Определение последнего элемента Определение последней интерации:
```smarty ```smarty
{foreach $list as $value} {foreach $list as $value}
<div>{if $value:last} last item {/if} {$value}</div> <div>{if $value@last} last item {/if} {$value}</div>
{/foreach}
или
{foreach $list as $value last=$last}
<div>{if $last} last item {/if} {$value}</div>
{/foreach} {/foreach}
``` ```