【问题标题】:How can I increment a Smarty variable?如何增加 Smarty 变量?
【发布时间】:2011-06-03 18:21:40
【问题描述】:

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

我想回显一个数组的索引,但我想在每次回显时递增它。

这就是我所拥有的……

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

它不起作用。

最好的方法是在将数组交给 Smarty 之前对其进行预处理吗?

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

【问题讨论】:

  • 但是既然你从 foreach 循环中得到index,你的意思是它实际上只是差了 1?
  • @mario 是的,这就是我想要的。
  • 您也可以使用{counter start=1} 代替索引。

标签: php smarty


【解决方案1】:

您可以执行以下操作:

<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}

【讨论】:

  • 如果我需要增加和减少计数器怎么办?
  • 如果页面上有多个 foreach 怎么办?计数将从0 再次开始。
【解决方案2】:

如果它是 smarty 2(从你使用的 foreach 语法来看),你可以给 foreach 循环一个名字,然后使用{$smarty.foreach.name.index}

像这样

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

索引从零开始,如果您想要一个从 1 开始的序列,请使用 .iteration 而不是 .index

我已经有一段时间没有使用 smarty 了,但我一直觉得官方文档非常好,有很多例子http://www.smarty.net/docsv2/en/language.function.foreach.tpl

【讨论】:

    【解决方案3】:

    $index++ 不会在回显后增加它吗?

    try ++$index;$index++ 在你回显之前。

    【讨论】:

      【解决方案4】:

      假设您通过 $foo 这是一个具有索引和迭代选项的数组

      {foreach from=$foo item=bar name=humbug}  
          {$smarty.foreach.humbug.index}
          {$smarty.foreach.humbug.iteration}
      {/foreach}
      

      第一列是索引结果,第二列是迭代结果

      0 - 1  
      1 - 2  
      2 - 3  
      3 - 4  
      4 - 5
      

      这意味着索引从 0 开始作为它的数组索引,其中迭代是从 1 开始的循环迭代计数。

      使用错误值会导致问题的一个实例是在表格中以 4 行或任何其他数量显示某些内容。

      使用 index 会导致表格布局不当。您将在循环的第一次迭代(索引 0)上立即获得行更改,这将在第 5 次迭代(索引 4)时自行纠正,但仅在当前布局的范围内,这意味着您的第一行将只有一个单元格它。每隔一行将有 4 个单元格,并且第一行之后每个单元格中的数据将出现在表中的 4 个单元格中。

      {if $smarty.foreach.humbug.index is div by 4}
          </tr><tr>
      {/if}
      

      使用 iteration 将正确布置行更改,在最后一次迭代或 foreach 循环之前给出相等的 4 行。

      {if $smarty.foreach.humbug.iteration is div by 4}
       </tr><tr>
      {/if}
      

      在 foreach 循环之后,您只需添加一个更接近的表格行即可完成最后一行。

      我希望这对某人有所帮助。

      【讨论】:

        【解决方案5】:
        {foreach from=$foo item=bar name=humbug}  
        {$smarty.foreach.humbug.index}
        {$smarty.foreach.humbug.iteration}
        {/foreach}
        

        {foreach from=$foo item=bar name=berlin}  
        {$smarty.foreach.berlin.index}
        {$smarty.foreach.berlin.iteration}
        {/foreach}
        

        【讨论】:

          【解决方案6】:

          你可以使用{counter}

          {counter} 用于打印计数。 {counter} 会记得 依靠每次迭代。您可以调整数量、间隔和 计数的方向,以及确定是否 打印值。您可以同时运行多个计数器 为每个人提供一个唯一的名称。如果您不提供名称,则 将使用名称“默认”

          来源:http://www.smarty.net/docsv2/en/language.function.counter.tpl

          用法:

          {counter start=0 print=false assign="count"}
          
          <ul>
           {foreach from=$listing.products item="product"}
              {counter}
              {if $count === 1}
               <p>Count is 1</p>
              {/if}
           {/foreach}
          </ul>
          

          【讨论】:

            猜你喜欢
            • 2012-01-30
            • 1970-01-01
            • 2018-04-02
            • 2017-07-03
            • 2015-09-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多