johan855 johan855 - 1 year ago 177
Smarty Question

Counter in smarty

I would like to assign values to a variable depending on a counter's value inside a FOR EACH loop.
So if counter=1, then I assign the value 'a', but if the counter is not 1, then I assign the value 'b'.

This is how far I have gotten but my code is breaking once I add the conditions and the counter change...

[{foreach from=$orderArticles item="currOrderArticle"}]
[{if $counter == 1}]
[{assign var="counter" value=2}]

Answer Source

Smarty already has a iteration counter for foreach loops

[{foreach $orderArticles as $currOrderArticle}]
  [{if $currOrderArticle@iteration == 1}]
   ... do something ...
  ... do something else ...

Read more about it in the Smarty documentation for foreach