The #compare block helper compares two variables using a logical operator.
{{#compare field operator field}} expr {{else}} expr {{/compare}}
The compare helper compares data variables against each other using logical operators. The "else" statement is used to output text if the argument returns a false condition.
|
Template |
{{#compare details.fromState "===" "NE"}}+{{details.qty}}
{{else}}{{details.qty}}{{/compare}}
{{#compare details.fromState "===" "AK"}}TRUE: {{details.qty}}
{{else}}FALSE{{/compare}}
|
|---|---|
|
Context |
{
"export": {},
"details": {
"fromState": "NE",
"customerName": "Thomas",
"customerId": "22222",
"qty": "3",
"other": " "
}
}
|
|
Output |
+3 FALSE |
The second argument is the arithmetic operator to be used for comparing the two arguments. Optionally, you can also specify the {{else}} expression to render the value when #compare returns FALSE.
In the following example, reversing the operator will change the output to “false.”
|
Template |
{{#compare sample1 ">" sample2}} true {{else}} false {{/compare}}
|
|---|---|
|
Context |
{
"sample1": "50",
"sample2": "100",
"sample3": "200"
}
|
|
Output |
true |
This example shows a comparison of the quantity on hand against the quantity ordered, and logical output.
|
Template |
{{#compare qty ">=" ordered}}Please ship {{qty}} units
{{else}} do-not-ship{{/compare}}
|
|---|---|
|
Context |
{
"state": "VA",
"name": "Thomas Jefferson",
"part": "R1",
"qty": "100",
"ordered": "100",
"inventoryOnOrder": "1000",
"ERP_Item": "kit"
}
|
|
Output |
Please ship 100 units |
This example uses compare to verify a phone number field has exactly 10 digits. If not, the output is ten zeros (0000000000).
|
Template |
{{#compare phone.length "===" 10}}{{else}}0000000000{{/compare}}
|
|---|---|
|
Context |
{
"phone": "123456789"
}
|
|
Output |
0000000000 |
|
Operator |
Description |
|---|---|
|
< |
Less than (a < b) |
|
> |
Greater than (a > b) |
|
<= |
Less than or equal to (a <= b) |
|
>= |
Greater than or equal to (a >= b) |
|
== |
Equal to (a == b) If the two compared values are not of the same type (string, boolean, or number), this operator will attempt to convert the values to like types. |
|
=== |
Strict equal to (a === b) If the two compared values are not of the same type (string, boolean, or number), this operator will NOT attempt to convert the values to like types. No type conversion is done, and the types must be the same to be considered equal. |
|
!= |
Not equal to (a != b) If the two compared values are not of the same type (string, boolean, or number), this operator will attempt to convert the values to like types. |
|
!== |
Strict not equal to (a !== b) If the two compared values are not of the same type (string, boolean, or number), this operator will NOT attempt to convert the values to like types. No type conversion is done, and the types must be the same to be considered equal. |
Note
You can reference literal strings in compare statements by surrounding the literal string in double quotation marks. For example: {{#compare record.Domain "==" "eu.integrator.io"}}
This example shows the #compare block helper with a nested compare expression. The sample compares the quantity order with current inventory levels. It then compares the current inventory levels with items on order. If the order can be successfully filled, the expressions return “Match”. If not, the output returns “Inventory levels too low”.
|
Template |
{{#compare ordered "<=" stockLevel}}Match{{else compare stockLevel "<"
inventoryOnOrder}}Match{{else}}Inventory levels too low{{/compare}}
|
|---|---|
|
Context |
{
"state": "NE",
"name": "Thomas",
"qty": "100",
"ordered": "100",
"stockLevel": "10",
"inventoryOnOrder": "1000",
"ERP_Item": "kit"
}
|
|
Output |
Match |