• Blogs (9)
    • 📱 236 - 992 - 3846

      📧 jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • 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.

    @keycurrent key of the loop item
    <ul>
     {foreach $hashArray as $value}
       <li>{$value@key}: {$value}</li>
     {/foreach}
    </ul>
    
    @indexcontains 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}
    
    @iterationcontains 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}
    
    @showcan 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}
    
    @totalcontains the number of iterations that this {foreach} will loop. 
    {foreach $items as $item}
      {$item.name}<hr/>
      {if $item@last}
        
    {$item@total} items
    {/if} {foreachelse} ... no items to loop ... {/foreach}