Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

smarty passing the value from {math} equatio to a variable

Have a nice day everyone, I have something to ask your hel, to better understand here is my code:

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

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

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

Hope you can help me guys!

like image 794
Noel Balaba Avatar asked Jun 22 '12 10:06

Noel Balaba


2 Answers

If you're using Smarty 3 I highly recommend ditching the {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 both better readable and faster (as the expression is actually compiled, rather than 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 there is any chance to upgrade to Smarty 3 - do it!

like image 126
rodneyrehm Avatar answered Oct 30 '22 14:10

rodneyrehm


Try to use assign parameter:

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

If you supply the assign attribute, the output 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 had made ​​similar calculations using PHP (at business-logic layer, not at view)

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

like image 34
Vladimir Posvistelik Avatar answered Oct 30 '22 13:10

Vladimir Posvistelik