William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Smary3 {foreach} properties summary

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}
    <div id="total">{$item@total} items</div>
  {/if}
{foreachelse}
 ... no items to loop ...
{/foreach}   
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: