如何增加Smarty变量?

ale*_*lex 13 php smarty

我通常不是一个Smarty人,所以我有点卡住了.

我想回显数组的索引,但我希望每次回显时都增加它.

这就是我所拥有的......

<ul>
    {foreach from=$gallery key=index item=image}
    <li>
        <img src="{$image}" alt="" id="panel-{$index++}" />
    </li>
    {/foreach}
</ul>
Run Code Online (Sandbox Code Playgroud)

它不起作用.

在将数组交给Smarty之前,这是预处理数组的最佳方法吗?

有没有办法可以使用Smarty做到这一点?

Rus*_*ias 23

您可以执行以下操作:

<ul>
    {foreach from=$gallery key=index item=image name=count}
    <li>
        <img src="{$image}" alt="" id="panel-{$smarty.foreach.count.index}" />
    </li>
    {/foreach}
</ul>
Run Code Online (Sandbox Code Playgroud)

从零开始,index是当前的数组索引.

然而,这可能是最好的方法,只需在你可以使用的循环之外使用一个计数器,如下所示:foreachcounter

{counter start=0 skip=1 assign="count"}
Run Code Online (Sandbox Code Playgroud)

要增加它,只需{counter}在每次迭代时调用.

{counter}
{*Can then use the $count var*}
   {if $count is div by 4}
      {*do stuff*}
   {/if}
Run Code Online (Sandbox Code Playgroud)