如何递增Smarty变量? [英] How can I increment a Smarty variable?

查看:23
本文介绍了如何递增Smarty变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常不是一个聪明的人,所以我有点卡住了。

我想回显数组的索引,但每次回显时都要递增它。

这就是我的……

<ul>
    {foreach from=$gallery key=index item=image}
    <li>
        <img src="{$image}" alt="" id="panel-{$index++}" />
    </li>
    {/foreach}
</ul>

它不起作用。

在将数组交给Smarty之前对其进行预处理的最佳方式是什么?

有什么方法可以使用Smarty完成此操作吗?

推荐答案

您可以执行以下操作:

<ul>
    {foreach from=$gallery key=index item=image name=count}
    <li>
        <img src="{$image}" alt="" id="panel-{$smarty.foreach.count.index}" />
    </li>
    {/foreach}
</ul>

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

这可能是最好的方法,但是,只需在foreach循环外使用counter就可以使用counter,如下所示:

{counter start=0 skip=1 assign="count"}

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

{counter}
{*Can then use the $count var*}
   {if $count is div by 4}
      {*do stuff*}
   {/if}

这篇关于如何递增Smarty变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆