Operators, Math/Calculations, and Functions

Throughout the DocuMerge app, and including your templates, you can use operators and functions to refine and transform your data for logic and display purposes. Below is a list of available operators and functions. For other formatting modifications, check out Modifiers. Here’s a few examples of basic calculations you can make within your document templates,…

Image Description

Throughout the DocuMerge app, and including your templates, you can use operators and functions to refine and transform your data for logic and display purposes. Below is a list of available operators and functions. For other formatting modifications, check out Modifiers.

Here’s a few examples of basic calculations you can make within your document templates, as well as fields within the DocuMerge application. These examples print the value (output the value in document templates or email delivery templates) after any transforming and calculating:

  • {($x / $y)|round:2}
  • {$x - $y + $_a}
  • {$x + ($_a + $_b)}

Instead of outputting the calculation, you can also create a new variable or set and existing variable. You can create and set variables within your templates and document/route fields (settings, rules, field maps, email templates, delivery fields, etc).

  • {$_subt = $price * $quantity}
  • {$_subt = $_subt - ($_subt * $discount)}
  • {$_total = $_subt + ($_subt * $taxrate)}{if $freeship != 1}{$_total = $_total + $ship}{/if}
  • {$_yousaved = ($_subt * $discount)|number_format:2}


Variables Notes:
We recommend you use an “_” when creating variables within the DocuMerge app. This will help you quickly determine variables that are locally created versus the variables created from your incoming data.
Also, variables set on the fly are not globally accessible during a merge process, thus variables created or overwriting incoming data will not pass along to other parts of of the merge process. IE: a variable created/set within a template is available for use during that merge within only that template, and only after it’s created/set. It’s not available in Rules or some other settings field. To create and set global variables, use the Document or Route Field Map.


LOGICAL

OperatorExample
and{if $_a > 1 and $x > 1)}
or{if $_a > 1 or $x > 1)}


ARITHMETIC

OperatorNameExample
+Addition{$_a = $x + $y}
Subtraction{$_a = $x - $y}
*Multiplication{$_a = $x * $y}
/Division{$_a = $x / $y}
%Modulus{$_a = $x % $y}
For more on math, visit: https://www.smarty.net/docs/en/language.math.tpl


ASSIGNMENT

OperatorExampleSame As
={$_a = $x}
+={$_a += $x}{$_a = $_a + $x}
-={$_a -= $x}{$_a = $_a – $x}
*={$_a *= $x}{$_a = $_a * $x}
/={$_a /= $x}{$_a = $_a / $x}


COMPARISON

OperatorNameExample
==Equal{if $x == $y}
===Identical{if $x === $y}
!=Not Equal{if $x != $y}
<>Not Equal{if $x <> $y}
!==Not Identical{if $x !== $y}
>Greater Than{if $x > $y}
<Less Than{if $x < $y}
>=Greater Than or Equal To{if $x >= $y}
<=Less Than or Equal To{if $x <= $y}


INCREMENT/DECREMENT

OperatorExampleSame As
++ (pre){$++x}{$x = $x + 1}{$x}
++ (post){$x++}{$x}{$x = $x + 1}
— (pre){$--x}{$x = $x – 1}{$x}
— (post){$x--}{$x}{$x = $x – 1}


FUNCTIONS

NameExampleDescription
isset{if isset($x)}If $x has value.
empty{if empty($x)}If $x is empty.
is_string{if is_string($x)}If $x is a string.
strtoupper{$_a = strtoupper($str)}Capitalize string.
strtolower{$_a = strtolower($str)}Lowercase string.
trim{$_a = trim($str)}Strip leading and trailing whitespace.
substr{$_a = substr($str, 5)}Removes last 5 characters.
str_replace{$_a = str_replace("Hi","Hey",$str)}Replaces “Hi” with “Hey”.
strstr{$_a = strstr($str,"Hi")}Returns string after 1st occurrence of “Hi”.
stristr{$_a = stristr($str,"Hi")}Case-insensitive version of the prior.
nl2br{$_a = nl2br($str)}Replaces \n breaks with <br /> breaks.
is_array{if is_array($x)}If $x is an array.
count{$_a = count($arr)}Returns number of elements in array.
explode{$_a = explode(" ",$str)}Breaks string into an array.
implode{$_a = implode(" ",$arr)}Creates string from array.
time{$_a = time()}Returns Unix timestamp of current time.
date{$_a = date("m-d-Y",$time)}Returns the date/time as specified.
strtotime{if strtotime("now")}Convert textual datetimes to Unix time.
json_encode{$_a = json_encode($ary)}Create JSON object from array.
json_decode{$_a = json_decode($obj)}Create object from JSON data.
Tags: , , ,

Was this article helpful to you?

Yes No

Related Articles