简体   繁体   中英

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?

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! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM