How can I check the current iteration for foreach and do something?
{foreach $new_products as $product name=foo}
{if $smarty.foreach.foo.iteration=5}
Do it!
{/if}
{/foreach}
This always return not checked
I think you should do {if $smarty.foreach.foo.iteration == 5}
(note the ==
).
There is an alternative (I think newer) technique for this. The example from the Smarty docs demonstrates it nicely:
{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 the index starts from zero, so index 3 is the 4th iteration.
For Smarty 3 you can use the @iteration property
{foreach $new_products as $product}
{if $product@iteration == 5}
Do it!
{/if}
{/foreach}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With