Articles in this section

#contains helper

The #contains block helper parses the name:value specified in the block to check for the presence of a given value. If the value specified is not present, then it prints the {{else}} expression.

{{#contains fieldArray field}} optionalTextIfTrue {{else}} optionalTextIfFalse {{/contains}}

The fieldArray value can be an array name, arrayname.keyname, or a string that will be interpreted as an array.

In the first example, the order.item value is “12345”. The specified value in the #contains block is “5”. Since 5 is a number present in the value, the first expression statement will be displayed. The second example is looking for a “6”. Since 6 is not present, the else statement it output.

Template

Context

Output

{{#contains order.item "5"}}
Sales ID Found!
{{else}}Sales ID Missing!
{{/contains}}
{
  "order": {
    "item": "12345"
  }
}
Sales ID Found!
{{#contains order.item "6"}}
Sales ID Found!
{{else}}Sales ID Missing!
{{/contains}}
{
  "order": {
    "item": "12345"
  }
}
Sales ID Missing!
{{#contains order.sales "2"}}
Sales ID Found!
{{else}}Sales ID Missing!
{{/contains}}
{
  "order": {
    "sales": ["2","3","59","4","9","10","7"]
  }
}
Sales ID Found!

The example below shows an if/then argument against the product and whether or not a product warranty was purchased via an if...else argument against the context fields specified.

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

The example below uses #contains expression nested inside of an if/else expression to set a field to true if another field contains a specific string. In this example, the source_name field contains "3.1415926". If the value for source_name does not contain "3.1415926", the field is set to false.

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