簡體   English   中英

在smarty for循環中添加var

[英]add var in smarty for loop

我有一個關於“ for”循環的問題。

我嘗試在每次迭代中添加var值,例如在php中的for循環中,如下所示:

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

echo $foo // 25

但是我需要在smarty中做同樣的事情,我試圖像這樣在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的事情嗎?

提前致謝!

實際上,您可以在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來顯示內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM