Articles in this section

#compare block helper

The #compare block helper compares two values with a comparison operator and renders its block when the comparison is true. Add an optional {{else}} block to render alternative content when the comparison is false. Builders use it in Handlebars templates — including field mappings, response mappings, HTTP request bodies, and dynamic API paths — to render content conditionally based on how two values relate.

Usage

{{#compare field operator field}}expr{{else}}expr{{/compare}}

First field -- The first value to compare. Can be a field reference or a literal value. Required.

operator -- The comparison operator, in double quotation marks. See Comparison operators. Required.

Second field -- The second value to compare. Wrap literal strings in double quotation marks. Required.

The {{else}} block is optional. Without it, the helper renders nothing when the comparison is false.

Comparison operators

Operator Description
< Less than.
> Greater than.
<= Less than or equal to.
>= Greater than or equal to.
== Equal to. When the values differ in type (string, boolean, or number), the helper converts them to the same type before comparing.
=== Strict equal to. The helper does not convert types; the values must be the same type to be equal.
!= Not equal to. When the values differ in type, the helper converts them to the same type before comparing.
!== Strict not equal to. The helper does not convert types before comparing.

Examples

Compare a field to a literal value

Each block compares details.fromState to a literal state code. The first matches NE, so it renders +3. The second checks AK, which doesn't match, so it renders the {{else}} block.

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

Compare two values with else

Because sample1 and sample2 are strings, the helper compares them as text rather than as numbers, so "50" is greater than "100" and the result is true. Reversing the operator to < returns false.

Template

{{#compare sample1 ">" sample2}} true {{else}} false {{/compare}}

Context

{
  "sample1": "50",
  "sample2": "100",
  "sample3": "200"
}

Output

true

Compare quantity values

This compares the quantity on hand (qty) with the quantity ordered (ordered). Because 100 is greater than or equal to 100, the helper renders the ship message.

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

Check a field's length

This checks whether phone has exactly 10 digits by comparing phone.length to 10. The number has 9 digits, so the comparison is false and the {{else}} block renders 10 zeros.

Template

{{#compare phone.length "===" 10}}{{else}}0000000000{{/compare}}

Context

{
  "phone": "123456789"
}

Output

0000000000

Chain conditions with else compare

Use {{else compare field operator field}} to test a second comparison when the first is false. This block first checks whether ordered is at most stockLevel. That comparison is false (100 is not less than or equal to 10), so the helper checks the next condition: stockLevel less than inventoryOnOrder. That comparison is true (10 is less than 1000), so the helper renders Match. If neither were true, it would render 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

Notes and constraints

  • Wrap literal strings in double quotation marks, for example {{#compare record.Domain "==" "eu.integrator.io"}}.
  • The {{else}} block is optional. Without it, the helper renders nothing when the comparison is false.
  • Use {{else compare field operator field}} to test another condition when the first comparison is false.
  • The helper compares string values as text, so numeric strings such as "50" and "100" may not compare the way the underlying numbers would. Use the same data types on both sides for predictable results.