7

I have this smarty code :

{iterate from=fruits item=fruit}
 ....
{/iterate}

I want to have a counter inside this loop that accept a start value and increase by one until the loop continues.

I should i use? i am not good in smarty.

Thank you.

hd.
  • 17,596
  • 46
  • 115
  • 165

2 Answers2

14

You can use the .iteration

{foreach from=fruits item=fruit}
 current item #: {$smarty.foreach.fruits.iteration}
{/foreach}

source: http://www.smarty.net/docsv2/en/language.function.foreach.tpl#foreach.property.index

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
3

In case you have something like this:

{foreach from=$fruits item=fruit} {/foreach}

Instead of this:

{foreach from=fruits item=fruit} {/foreach}

You should use this syntax:

{foreach from=$fruits item=fruit name=counter} 
    Iteration no {$smarty.foreach.counter.iteration}
{/foreach}
paulalexandru
  • 9,218
  • 7
  • 66
  • 94