Smarty passing a value from {math} equatio to a variable

Have a nice day every, I have something to ask you, to better understand, here is my code:

{math equation=((($order_total-$commission)+$discount+$delivery_charge)*$rate)} 

I want this to go to another variable, in php I want to be like that

 <?php $var=0 foreach($result as $resultA){ $var = $var+((($order_total-$commission)+$discount+$delivery_charge)*$rate); } ?> 

Hope you help me guys!

+4
source share
2 answers

If you are using Smarty 3, I highly recommend writing {math}:

 {$order_total = 123} {$commission = 13} {$discount = 10} {$delivery_charge = 20} {$rate = 1.1} {$result = 0} {$result = $result + ($order_total - $commission + $discount + $delivery_charge) * $rate} {$result} 

It is better read and faster (since the expression is really compiled, not eval()ed over and over again).


Smarty 2 equivalent:

 {assign var="order_total" value=123} {assign var="commission" value=13} {assign var="discount" value=10} {assign var="delivery_charge" value=20} {assign var="rate" value=1.1} {assign var="result" value=0} {math assign="result" equation="result + (order_total - commission + discount + delivery_charge) * rate" result=$result order_total=$order_total commission=$commission discount=$discount delivery_charge=$delivery_charge rate=$rate } {$result} 

If you can upgrade to Smarty 3 - do it!

+11
source

Try using the assign parameter:

From the documentation http://www.smarty.net/docsv2/en/language.function.math.tpl :

If you specify the assign attribute, the result of the {math} function will be assigned to this template variable instead of being output to the template.

But it would be better if you did such calculations using PHP (at the level of business logic, not at the presentation)

See http://www.smarty.net/best_practices (section "Separate business logic!" )

0
source

All Articles