Skip to main content

Comparison Operators

When we need to compare values from one another, these operators will help with your markdown.

List of Operators

{{ and }}

Helper that renders the block if both of the given values are truthy. If an inverse block is specified it will be rendered when falsy. Works as a block helper, inline helper or subexpression.

Params

* `a` **{any}**
* `b` **{any}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**

Example

<!-- {great: true, magnificent: true} -->
{{#and great magnificent}}A{{else}}B{{/and}}
<!-- results in: 'A' -->

{{ compare }}

Render a block when a comparison of the first and third arguments returns true. The second argument is the arithemetic operator to use. You may also optionally specify an inverse block to render when falsy.

Params

* `a` **{}**
* `operator` **{}**: The operator to use. Operators must be enclosed in quotes: `">"`, `"="`, `"<="`, and so on.
* `b` **{}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or if specified the inverse block is rendered if falsey.

{{ contains }}

Block helper that renders the block if collection has the given value, using strict equality (===) for comparison, otherwise the inverse block is rendered (if specified). If a startIndex is specified and is negative, it is used as the offset from the end of the collection.

Params

* `collection` **{Array|Object|String}**: The collection to iterate over.
* `value` **{any}**: The value to check for.
* `[startIndex=0]` **{Number}**: Optionally define the starting index.
* `options` **{Object}**: Handlebars provided options object.

Example

<!-- array = ['a', 'b', 'c'] -->
{{#contains array "d"}}
This will not be rendered.
{{else}}
This will be rendered.
{{/contains}}

{{ default }}

Returns the first value that is not undefined, otherwise the "default" value is returned.

Params

* `value` **{any}**
* `defaultValue` **{any}**
* `returns` **{String}**

{{ eq }}

Block helper that renders a block if a is equal to b. If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value.

Params

* `a` **{String}**
* `b` **{String}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.

Example

<!-- when agreement type is renewal -->
{{#eq deal.contract.formType "renewal"}}
Thank you for your continued business!
{{/eq}}
<!-- when agreement type is new order -->
{{#eq deal.contract.formType "new-order"}}
We're excited to start this partnership!
{{/eq}}

{{ gt }}

Block helper that renders a block if a is greater than b.

If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value.

Params

* `a` **{String}**
* `b` **{String}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.

Example

{{#gt 2 1}} Two is greater than One. {{/gt}}

{{ gte }}

Block helper that renders a block if a is greater than or equal to b.

If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value.

Params

* `a` **{String}**
* `b` **{String}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.

Example

{{#gte 12 deal.contract.length}} This agreement extends over a year! {{/gte}}

{{ has }}

Block helper that renders a block if value has pattern. If an inverse block is specified it will be rendered when falsy.

Params

* `val` **{any}**: The value to check.
* `pattern` **{any}**: The pattern to check for.
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**

{{ ifEven }}

Return true if the given value is an even number.

Params

* `number` **{Number}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.

Example

{{#ifEven value}}
render A
{{else}}
render B
{{/ifEven}}

{{ ifNth }}

Conditionally renders a block if the remainder is zero when a operand is divided by b. If an inverse block is specified it will be rendered when the remainder is not zero.

Params

* **{}**: {Number}
* **{}**: {Number}
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.

{{ ifOdd }}

Block helper that renders a block if value is an odd number. If an inverse block is specified it will be rendered when falsy.

Params

* `value` **{Object}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.

Example

{{#ifOdd value}}
render A
{{else}}
render B
{{/ifOdd}}

{{ isFalsey }}

Returns true if the given value is falsey. Uses the falsey library for comparisons. Please see that library for more information or to report bugs with this helper.

Params

* `val` **{any}**
* `options` **{Options}**
* `returns` **{Boolean}**

{{ isTruthy }}

Returns true if the given value is truthy. Uses the falsey library for comparisons. Please see that library for more information or to report bugs with this helper.

Params

* `val` **{any}**
* `options` **{Options}**
* `returns` **{Boolean}**

{{ is }}

Block helper that renders a block if a is equal to b. If an inverse block is specified it will be rendered when falsy. Similar to eq but does not do strict equality.

Params

* `a` **{any}**
* `b` **{any}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**

{{ isnt }}

Block helper that renders a block if a is not equal to b. If an inverse block is specified it will be rendered when falsy. Similar to unlessEq but does not use strict equality for comparisons.

Params

* `a` **{String}**
* `b` **{String}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**

{{ lt }}

Block helper that renders a block if a is less than b.

If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value.

Params

* `context` **{Object}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.
{{#lt 12 deal.contract.length}} This agreement is less than a year long. {{/lt}}

{{ lte }}

Block helper that renders a block if a is less than or equal to b.

If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value.

Params

* `a` **{Sring}**
* `b` **{Sring}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.

Example

{{#lte 4 40}} 4 is less than 40 {{/lte}}

{{ neither }}

Block helper that renders a block if neither of the given values are truthy. If an inverse block is specified it will be rendered when falsy.

Params

* `a` **{any}**
* `b` **{any}**
* `options` **{}**: Handlebars options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.

{{ not }}

Returns true if val is falsey. Works as a block or inline helper.

Params

* `val` **{String}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**

{{ or }}

Block helper that renders a block if any of the given values is truthy. If an inverse block is specified it will be rendered when falsy.

Params

* argument  **{...any}**: Variable number of arguments
* options **{Object}**: Handlebars options object
* returns **{String}**: Block, or inverse block if specified and falsey.

Example

{{#or a b c}}
If any value is true this will be rendered.
{{/or}}

{{ switch }}

Switch is a block operator, {{#switch}}{{/switch}} that allows a user to control what to render based on value or values defined in a {{#case}}{{/case}} or values that do not match any case expressions to a {{#default}}{{/default}} block.

You can read more about what a Switch statement is on wikipedia.

Params

* `value` **{String|Number}**: String or number to match. This field is type-sensitive.
* `returns` **{String}**: Returns evaluated `{{case}}` and/or `{{default}}` expression.

Example

{{#switch current.sku.quantity}}
{{#case 1}}Purchasing 1 unit.{{/case}}
{{#case 2 3 4 5 6 7 8 9}}Purchasing under 10 units.{{/case}}
{{#default}}Purchasing more than 10 units{{/default}}
{{/switch}}

{{ case }}

Case defines an equality expression for a Switch, returning the content between the case block on a match. {{case}} can only be used within a {{#switch}}{{/switch}} statement.

You can have as many {{case}} statements next to each other, and {{default}} often follows the last {{case}} in a {{switch}} statement.

Params

* `match` **{String|Number}**: Matching value to compare with  `{{switch}}` value.
* `returns` **{String}**: Returns evaluated `{{case}}` when `match === value` of `{{switch}}`.

Example

Match on string

{{#case "yes"}}Yes{{/case}}
{{#case "No"}}Yes{{/case}}

Match on number or list of numbers.

{{#case 1}}One{{/case}}
{{#case 2 3 4}}One, Two, or Three{{/case}}

{{ unlessEq }}

Block helper that always renders the inverse block unless a is is equal to b.

Params

* `a` **{String}**
* `b` **{String}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Inverse block by default, or block if falsey.

{{ unlessGt }}

Block helper that always renders the inverse block unless a is is greater than b.

Params

* `a` **{Object}**: The default value
* `b` **{Object}**: The value to compare
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Inverse block by default, or block if falsey.

{{ unlessLt }}

Block helper that always renders the inverse block unless a is is less than b.

Params

* `a` **{Object}**: The default value
* `b` **{Object}**: The value to compare
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.

{{ unlessGteq }}

Block helper that always renders the inverse block unless a is is greater than or equal to b.

Params

* `a` **{any}**
* `b` **{any}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.

{{ unlessLteq }}

Block helper that always renders the inverse block unless a is is less than or equal to b.

Params

* `a` **{any}**
* `b` **{any}**
* `options` **{Object}**: Handlebars provided options object
* `returns` **{String}**: Block, or inverse block if specified and falsey.