Search code examples
phpsmartycounter

How to do counter with smarty?


I want to do one counter with Smarty.

In php I do $i++ to create a counter and after I use if() with if($i%3 == 0).

How could I do the same but with Smarty?


Solution

  • To initialise a counter in Smarty, you just need to specify the variable, and optionally provide the start point and skip (incremental offset):

    {counter start=0 skip=3 assign=var} // Initialise the counter **and** log the first output
    {counter}<br />
    {counter}<br />
    {counter}<br />
    

    This will start a counter at 1, and increment by 3, so this will output:

    1<br />
    4<br />
    7<br />
    10<br />
    

    Then you can check if a particular number in the counter is divisible by three with:

    {if $var is div by 3}
       ...
    {/if}
    

    Hope this helps! :)