Smary3 {foreach} properties summary
Blogs20122012-12-20
Smary3 {foreach} properties summary
Here I summary Smary3 {foreach} properties summary for quick retriving. For details, see http://www.smarty.net/docs/en/language.function.foreach.tpl.
| @key | current key of the loop item | <ul>
{foreach $hashArray as $value}
<li>{$value@key}: {$value}</li>
{/foreach}
</ul>
|
| @index | contains the current array index, starting with 0. | {foreach $items as $i}
{if $i@index eq 3} <tr> {/if}
<td>{$i.label}</td>
{if $i@index eq 3} </tr> {/if}
{/foreach}
|
| @iteration | contains the current loop iteration and always starts at 1, unlike @index. | {foreach $myNames as $name}
{if $name@iteration is even by 3}
<span style="color: #000">{$name}</span>
{else}
<span style="color: #eee">{$name}</span>
{/if}
{/foreach}
|
| @first | @first is TRUE if the current {foreach} iteration is the initial 1. | <table>
{foreach $items as $i}
{if $i@first}
<tr>
<th>key</td>
<th>name</td>
</tr>
{/if}
<tr>
<td>{$i@key}</td>
<td>{$i.name}</td>
</tr>
{/foreach}
</table>
|
| @last | @last is set to TRUE if the current {foreach} iteration is the final one. | {foreach $items as $item}
<a href="#{$item.id}">{$item.name}</a>
{if $item@last}
<hr>
{else}
,
{/if}
{foreachelse}
... no items to loop ...
{/foreach}
|
| @show | can be used after the execution of a {foreach} loop. @show is a boolean value. | <ul>
{foreach $myArray as $name}
<li>{$name}</li>
{/foreach}
</ul>
{if $name@show}
do something here if the array contains data
{/if}
|
| @total | contains the number of iterations that this {foreach} will loop. | {foreach $items as $item}
{$item.name}<hr/>
{if $item@last}
|
