How to check iteration in smarty?

How to check current foreach iteration and do something?

{foreach $new_products as $product name=foo} {if $smarty.foreach.foo.iteration=5} Do it! {/if} {/foreach} 

It always returns unverified

+6
source share
3 answers

I think you should do {if $smarty.foreach.foo.iteration == 5} (pay attention to == ).

+9
source

There is an alternative (I think a newer) technique for this. An example from Smarty docs demonstrates this beautifully:

 {foreach $items as $i} {if $i@index eq 3} {* put empty table row *} <tr><td>nbsp;</td></tr> {/if} <tr><td>{$i.label}</td></tr> {/foreach} 

Note that the index starts at zero, so index 3 is the 4th iteration.

+6
source

For Smarty 3, you can use the @iteration property

 {foreach $new_products as $product} {if $product@iteration == 5} Do it! {/if} {/foreach} 
0
source

Source: https://habr.com/ru/post/909979/


All Articles