php 如何增加 Smarty 变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4600175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:38:23  来源:igfitidea点击:

How can I increment a Smarty variable?

phpsmarty

提问by alex

I am not usually a Smarty guy, so I'm a bit stuck.

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

I want to echo the index of an array, but I want to increment it each time I echo it.

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

This is what I have...

这就是我所拥有的...

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

It doesn't work.

它不起作用。

Is the best way to do this to pre-process the array before handing it to Smarty?

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

Is there a way I can do this using Smarty?

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

回答by Russell Dias

You can do something like the following:

您可以执行以下操作:

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

Starting from zero, indexis the current array index.

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

That's probably the best way to go about it, however, to simply use a counteroutside of a foreachloop you can use counter, like so:

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

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

To increment it simply call {counter}at each iteration.

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

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

回答by acqu13sce

If it's smarty 2 (which from the foreach syntax you're using it looks like) you can give the foreach loop a name and then use {$smarty.foreach.name.index}

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

like so

像这样

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

The index starts at zero, if you want a sequence that starts at 1 use .iteration instead of .index

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

I haven't used smarty for quite a while now but I always found the official documentation very good with lots of examples http://www.smarty.net/docsv2/en/language.function.foreach.tpl

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

回答by AnthonyHurst

wouldn't the $index++increment it after the echo?

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

try ++$index;or do $index++BEFORE you echo it.

try ++$index;或者$index++在你回应之前做。

回答by Chris

Assuming you run through $foo which is an array with the index and iteration options

假设您运行 $foo 这是一个带有索引和迭代选项的数组

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

The first column are the index results, the second column are the iteration results

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

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

This means index starts at 0 as its the array index, where as the iteration is the loop iteration count which begins at 1.

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

An instance where using the wrong value would cause problems is in displaying something in rows of 4 or any other amount in a table.

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

Using indexwould cause a badly laid out table. You would get an immediate row change on the first iteration of the loop (index 0) which would correct itself at the 5th iteration (index 4) but only within the scope of the current layout, meaning your first row would only have 1 cell in it. every other row would have 4 cells and the data in every cell after the first row would be appearing in the table 4 cells later than it should be doing.

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

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

Using iterationwould lay out the row change properly giving equal rows of 4 until the last iteration or the foreach loop.

使用迭代将适当地布置行更改,给出相等的 4 行,直到最后一次迭代或 foreach 循环。

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

After the foreach loop you would simply add a table row closer to complete the final row.

在 foreach 循环之后,您只需添加一个表行来完成最后一行。

I hope this helps somebody out.

我希望这可以帮助某人。

回答by user215174

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

or

或者

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

回答by Sébastien Gicquel

You can use {counter}

您可以使用 {counter}

{counter}is used to print out a count. {counter}will remember the count on each iteration. You can adjust the number, the interval and the direction of the count, as well as determine whether or not to print the value. You can run multiple counters concurrently by supplying a unique name for each one. If you do not supply a name, the name “default” will be used

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

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

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

Usage :

用法 :

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