在smarty for循环中添加var

时间:2014-08-19 13:59:47

标签: php for-loop smarty add

我有一个关于聪明的“for”循环的问题。

我尝试在每次迭代中添加var值,就像在php中的for循环一样:

for($i=0; $i<5; $i++)
{
   $foo += 5;
}

echo $foo // 25

但是我需要在smarty中做同样的事情,我试图在这样的for循环中添加值:

{for $i=0 to {$sale_info[record].id_payment_type|@count}-1}             
   {if {$sale_info[record].id_payment_type[{$i}]} eq "1" }                                  
       {math equation="(x + x)" x={$sale_info[record].prices[{$i}]}}
   {/if}
{/for}

但我没有得到结果。在smarty中可以做x + = 20这样的事情吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

事实上,你可以在Smarty做这样的事情。但是Smarty中没有for循环。

有一个简单的例子:

{assign var="y" value=0}
{section name=sec start=0  loop=5}
    {math equation="(x + x + y)" x =$smarty.section.sec.iteration y=$y assign="y"}
    {$y}<br />
{/section}

进行计算并将其分配到$y变量。

此输出将为:

2 // 1 + 1 + 0
6  // 2 + 2 + 2
12 // 3 + 3 + 6
20 // 4 + 4 + 12
30 // 5 + 5  + 20

但是,如果可能合理,我仍然建议您在PHP中进行复杂的计算,并仅使用Smarty来显示内容。

相关问题