Articles in this section

#contains block helper

The #contains block helper checks whether a value is present in an array or string, and renders one of two blocks based on the result. Builders use it inside Handlebars templates — including field mappings, response mappings, HTTP request bodies, and dynamic API paths — to conditionally include content depending on whether a field holds a specific value.

If the value is present, the helper renders the block before {{else}}. If the value is absent, it renders the block after {{else}}. The {{else}} block is optional; without it, the helper renders nothing when the value is absent.

Usage

{{#contains array value}}
  textIfPresent
{{else}}
  textIfAbsent
{{/contains}}

array -- The array or string to search. Accepts an array name, an arrayname.keyname path, or a string, which the helper interprets as an array before searching. Required.

value -- The value to look for. Wrap literal values in quotation marks, for example "5". Required.

Examples

Check a string for a value

A string argument is interpreted as an array, so #contains checks whether the value appears in it. With order.item set to 12345, searching for 5 renders the first block because 5 is present, and searching for 6 renders the {{else}} block because 6 is absent.

Template (value present)

{{#contains order.item "5"}}
Sales ID found
{{else}}
Sales ID missing
{{/contains}}

Template (value absent)

{{#contains order.item "6"}}
Sales ID found
{{else}}
Sales ID missing
{{/contains}}

Context

{
  "order": {
    "item": "12345"
  }
}

Output — Sales ID found for the first template, Sales ID missing for the second.

Check an array for a value

When the argument is an array, #contains checks whether the value is one of its elements. With order.sales set to ["2", "3", "59", "4", "9", "10", "7"], searching for 2 renders the first block because 2 is an element of the array.

Template

{{#contains order.sales "2"}}
Sales ID found
{{else}}
Sales ID missing
{{/contains}}

Context

{
  "order": {
    "sales": ["2", "3", "59", "4", "9", "10", "7"]
  }
}

Output

Sales ID found

Combine multiple checks

You can place more than one #contains block in the same template, and each block can omit {{else}}. In this example, the first block checks whether ERP_Item contains kit and the second checks whether warranty contains yes.

Template

{{#contains ERP_Item "kit"}}+1{{else}}None found{{/contains}}
{{#contains warranty "yes"}}Warranty included{{/contains}}

Context

{
  "state": "NE",
  "name": "Thomas",
  "id": "22222",
  "qty": "100",
  "ordered": "100",
  "stockLevel": "100",
  "returned": "100",
  "other": " ",
  "ERP_Item": "kit",
  "warranty": "yes"
}

Output

+1
Warranty included

Nest #contains inside an #if block

You can nest #contains inside an #if block to act only when a field exists. In this example, #if confirms that source_name has a value, and #contains then sets the output to true when source_name contains 3.1415926 and false when it doesn't.

Template

{{#if source_name}}{{#contains source_name "3.1415926"}}true{{else}}false{{/contains}}{{/if}}

Context

{
  "source_name": "3.1415926",
  "id": 123,
  "name": "Bob",
  "age": 33
}

Output

true

Notes and constraints

  • The {{else}} block is optional. Without it, the helper renders nothing when the value is absent.
  • Wrap literal search values in quotation marks, for example "yes".
  • A string argument is interpreted as an array before the check, so the helper can match a value within a string as well as an element within an array.