如何在smarty中检查迭代?

sky*_*ind 8 foreach smarty

如何查看foreach的当前迭代并执行某些操作?

{foreach $new_products as $product name=foo}
    {if $smarty.foreach.foo.iteration=5}
        Do it!
    {/if}
{/foreach}
Run Code Online (Sandbox Code Playgroud)

这总是返回未检查

apf*_*box 10

我想你应该这样做{if $smarty.foreach.foo.iteration == 5}(注意==).


Hig*_*lar 6

对此有一种替代方法(我认为更新).来自Smarty文档的示例很好地证明了这一点:

{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}
Run Code Online (Sandbox Code Playgroud)

请注意,索引从零开始,因此索引3是第4次迭代.