Here are some steps to take if you need to do some basic calculations within your document or other settings in WebMerge. For example, you have created an order form, but you need to tack on an additional processing fee that is not calculated by your form.

Here are some examples of some basic calculations. These simply print the result:

- {($Variable1*$Variable2)|number_format:2}
- {$Variable1+$Variable2}

If you need to save the values for later, you can use the assign function:

- {$Variable3 = $Variable1+$Variable2}
- {$Total = $Subtotal + $Shipping + $Tax}
- {$Discount = ($Subtotal * 0.25)|number_format:2}
- {$Percent = (100 * $Wins / $TotalGames)|number_format:2}

For more information on the mathematical calculations you can do within your documents, check out http://www.smarty.net/docs/en/language.math.tpl

## 12 Comments

Lee Kolmhi Jeremy, After looping through my table to show all the line items, i want to get a total at the bottom. I'm trying this, but i suspect there is a syntax error as i am only getting the last item in the array rather than the total. Help?

Total Order Amount: {foreach from=$IODetails item=_iodetail}{assign var=SumSales value=$_iodetail.Revenue__c + SumSales }{/foreach} ${$SumSales}

Jeremy ClarkeHi Lee,

Try this instead:

{foreach from=$IODetails item=_iodetail}{$SumSales = $_iodetail.Revenue__c + $SumSales}{/foreach} ${$SumSales}

Rajasekhar VKHi Jeremy, I have used auto-numbering to get the value of a variable. I want to capture this value for use later in the document. How do I do it? The field value that I want to capture runs like this:

A copy of the Certificate of Incorporation is annexed as Annexure A-{$annexure++}.

I defined it like this: {$A1= Annexure A-{$annexure++}}. But I get a compilation error. Please help..

Rajasekhar VKThis is the error I am getting:

Error merging document: Syntax Error in template "string:" "{$A1 = " - Unexpected "

Jeremy ClarkeHi Rajasekhar,

Simply do this: {$A1 = "Annexure A-"|cat:($annexure + 1)}

If you need more specific help, please create a support ticket.

Thanks!

Rajasekhar VKHi Jeremy

Thank you for the prompt response. Let me test and get back to you.

Thanks once again.

David AdamsHi Jeremy,

I want to find the number of days between two dates. I tried using the date_diff function but it says it is not allowed by security settings. Is there another way to do it? If date _diff is still the way to go, how would i actually write the function (including formatting it as days)?

Jeremy ClarkeHi David,

The easiest thing to do is this in your template:

{round((strtotime($Date1) - strtotime($Date2)) / (3600*24))} days

David AdamsWorked Perfectly, thanks for the assistance!

Mark BalsamIn my field map I want to add two different pieces of data coming in. I have entered it as follows in the field map: {if !is_array($MemberDeals)}{$MemberDeals = json_decode($MemberDeals, true)}{/if}{$MemberDeals.0.Square_Feet}+{if !is_array($MemberDeals)}{$MemberDeals = json_decode($MemberDeals, true)}{/if}{$MemberDeals.1.Square_Feet}

However, on the resulting merge PDF it is showing up as "50+16" as opposed to adding the two numbers to make "66". Any thoughts how to fix this? Thanks!

Jeremy ClarkeMark,

Your calculation needs to be inside the curly brackets like this:

{$MemberDeals.0.Square_Feet + $MemberDeals.1.Square_Feet}

Mark Balsamas usual, thanks!