Handlebars helper reference
Build templates that dynamically reference policy and related data
Overview
Predefined handlebars helpers allow you to manipulate merge variable data before it is injected into the template output. There are two categories of helpers, simple helpers and block helpers.
Simple helpers
Simple helpers result in a single expression when they are computed. Example use cases include arithmetic, manipulating dates, and formatting various data types.
{{add 2 3}} // Result: 5
There are five kinds of simple helpers, related to the merge variable data types they reference.
- Number helpers - For working with numeric data such as integers and floating point values, including basic arithmetic and formatting. These helpers can also be used for currency values represented as integers.
- Currency helpers - For formatting currency values. Currency values are stored on the Root platform as integers in cents.
- Date helpers - For working with dates, including date manipulation and displaying date values in a human readable format.
- Text helpers - For formatting text data.
- Policy document helpers - For adding links to policy documents.
Block helpers
Block helpers are useful for rendering blocks of text dependent on merge variable values. Blocks of text are wrapped in opening and closing handlebars expressions. The opening expression is denoted by #
(for example {{#if ... }}
or {{#each ...}}
), and the closing expression by /
(for example {{/if}}
or {{/each}}
).
There are two kinds of block helpers:
- Conditional helpers - For making the display of blocks of text conditional on merge variable values.
- Context helpers - For repeating blocks of text or changing the context within the merge variables in which handlebars expressions are evaluated.
Nesting helpers
You can combine multiple helpers to create complex logic and refer to multiple merge variable fields in the same expression. Helpers are combined by replacing an argument in one handlebars expression with another handlebars expression. The nested expression is enclosed in round parentheses.
The handlebars expressions will be evaluated from the innermost parentheses outwards. Handlebars expressions can be nested to an arbitrary depth.
Example: date manipulation
In the example below, the inner dateAdd
helper is evaluated first, and returns an integer representing a UNIX timestamp. This result is then passed as an argument to the dateFormat
helper.
{
"policy": {
"start_date": "2021-05-19"
}
}
{{dateFormat (dateAdd policy.start_date months="7") format="DD MMMM YYYY"}}
==> {{dateFormat 1639872000000 format="DD MMMM YYYY"}}
==> 19 December 2021
Example: arithmetic
{{add 3 (multiply 6 (divide 20 (subtract 5 3)))}}
==> {{add 3 (multiply 6 (divide 20 2))}}
==> {{add 3 (multiply 6 10)}}
==> {{add 3 60}}
==> 63
Example: comparison and logical operators
The if
helper allows you to compare values and combine expressions using logical operators. The example below checks whether the policy started more than 30 days ago and has a status of active
(the example assumes an evaluation date of 2021-08-01
).
{
"policy": {
"start_date": "2021-05-01",
"status": "active"
}
}
{{#if (if (dateDifference dateNow policy.start_date
measurement="days") ">" 30) "&&" (if policy.status "===" "active")}}
<p>The start date is more than 30 days ago and the policy is active.</p>
{{else}}
<p>The start date is less than 30 days ago or the policy is inactive.</p>
{{/if}}
================>
{{#if (if 92 ">" 30) "&&" true}}
<p>The start date is more than 30 days ago and the policy is active.</p>
{{else}}
<p>The start date is less than 30 days ago or the policy is inactive.</p>
{{/if}}
================>
{{#if true "&&" true}}
<p>The start date is more than 30 days ago and the policy is active.</p>
{{else}}
<p>The start date is less than 30 days ago or the policy is inactive.</p>
{{/if}}
================>
<p>The start date is more than 30 days ago and the policy is active.</p>
Number helpers
add
add
Returns the sum of two numbers.
Syntax
{{add <number1> <number2>}}
Parameters
number1
: Number.number2
: Number.
Returns
A Number equal to number1 + number2
.
Example
{{add 2 3}} // Result: 5
subtract
subtract
Returns the difference of two numbers.
Syntax
{{subtract <number1> <number2>}}
Parameters
number1
: Number.number2
: Number.
Returns
A Number equal to number1 - number2
.
Example
{{subtract 2 3}} // Result: -1
multiply
multiply
Returns the product of two numbers.
Syntax
{{multiply <number1> <number2>}}
Parameters
number1
: Number.number2
: Number.
Returns
A Number equal to number1 * number2
.
Example
{{multiply 2 3}} // Result: 6
divide
divide
Returns the quotient of two numbers.
Syntax
{{divide <number1> <number2>}}
Parameters
number1
: Number.number2
: Number.
Returns
A Number equal to number1 / number2
.
Example
{{divide 6 2}} // Result: 3
divide
divide
Returns the quotient of two numbers.
Syntax
{{divide <number1> <number2>}}
Parameters
number1
: Number.number2
: Number.
Returns
A Number equal to number1 / number2
.
Example
{{divide 6 2}} // Result: 3
round
round
Rounds a number to the specified number of decimal places.
Syntax
{{round <number> places=<places>}}
Parameters
number
: Number (or String representing a Number). The number to round.places
: Optional Integer (or String representing an Integer). The number of decimal places to round to. Defaults to0
.
Returns
A String representing the number rounded to the specified number of decimal places
Example
{{round 45.34 }} // Result: 45
{{round 45.34 places=1 }} // Result: 45.3
{{round 45.34 places=3 }} // Result: 45.340
{{round 45.34 places=-1 }} // Result: 50
roundup
roundup
Rounds up a number to the specified number of decimal places.
Syntax
{{roundup <number> places=<places>}}
Parameters
number
: Number (or String representing a Number). The number to round up.places
: Optional Integer (or String representing an Integer). The number of decimal places to round to. Defaults to0
.
Returns
A String representing the number rounded to the specified number of decimal places
Example
{{roundup 45.34 }} // Result: 46
{{roundup 45.34 places=1 }} // Result: 45.4
{{roundup 45.34 places=3 }} // Result: 45.340
{{roundup 45.34 places=-1 }} // Result: 50
rounddown
rounddown
Rounds down a number to the specified number of decimal places.
Syntax
{{rounddown <number> places=<places>}}
Parameters
number
: Number (or String representing a Number). The number to round down.places
: Optional Integer (or String representing an Integer). The number of decimal places to round to. Defaults to0
.
Returns
A String representing the number rounded to the specified number of decimal places
Example
{{rounddown 45.67 }} // Result: 45
{{rounddown 45.67 places=1 }} // Result: 45.6
{{rounddown 45.67 places=3 }} // Result: 45.670
{{rounddown 45.67 places=-1 }} // Result: 40
maxValue
maxValue
Returns the maximum of two numbers.
Syntax
{{maxValue <number1> <number2>}}
Parameters
number1
: Number.number2
: Number.
Returns
A Number equal to Math.max(number1, number2)
.
Example
{{maxValue 30000 60000}} // Result: 60000
ordinalString
ordinalString
Returns the ordinal representation of a number as a String. Floating point numbers are rounded down to the nearest integer.
Syntax
{{ordinalString <number>}}
Parameters
number
: Number.
Returns
A String.
Example
{{ordinalString 6}} // Result: 6th
numberToWords
numberToWords
Returns a String representing a number written out in words. Floating point numbers are rounded up to the nearest integer.
Syntax
{{numberToWords <number>}}
Parameters
number
: Number.
Returns
A String.
Example
{{numberToWords 50}} // Result: fifty
Currency helpers
formatCurrency
formatCurrency
Transforms a currency value in cents into full currency units by dividing by 100. Adds a three-digit currency code and formats the value for the specified locale using the JavaScript Number.prototype.toLocaleString()
method.
Syntax
{{formatCurrency <number> <currencyCode> locale=<locale> currencyDisplay=<currencyDisplay>}}
Parameters
number
: Integer. The currency value in cents.currencyCode
: String. Optional. The three-digit currency code, e.g"USD"
or"ZAR"
. Defaults to"ZAR"
.
Options
locale
: String. Optional. The reader's locale. Defaults to"en-ZA"
. Read more about the JavaScript implementation of locales.currencyDisplay
: String. Optional. How to display the currency. One of"symbol"
(default, use a localised currency symbol such as €),"narrowSymbol"
(use a narrow format symbol, e.g. "$100" rather than "US$100"),"code"
(use the ISO currency code) or"name"
(use a localised currency name such as "dollar").
Returns
A String.
Example
{{formatCurrency 2400 "GBP" locale="en-GB"}} // Result: £24.00
{{formatCurrency 2400 "USD" locale="en-US"}} // Result: $24.00
{{formatCurrency 2400 "USD" locale="en-GB"}} // Result: US$24.00
{{formatCurrency 2400 "USD" locale="en-US" currencyDisplay="code"}} // Result: USD 24.00
{{formatCurrency 2400 "ZAR" locale="en-ZA" currencyDisplay="name"}} // Result: 24,00 South African rand
{{formatCurrency 2400 "ZAR" locale="en-ZA" currencyDisplay="symbol"}} // Result: R 24,00
zarString
zarString
Transforms a South African rand value in cents into full currency units by dividing by 100. Adds the symbol R
before the value.
Syntax
{{zarString <number>}}
Parameters
number
: Integer. The currency value in cents.
Returns
A String.
Example
{{zarString 2400}} // Result: R 24.00
Date helpers
dateNow
dateNow
Returns the current date in the specified format.
Syntax
{{dateNow format=<format>}}
Options
format
: String. Optional. Tokens representing the date format. Defaults toDD MMMM YYYY
. See the moment.js docs for a list of available date formats.
Returns
A String representing the current date.
{{dateNow format="YYYY-MM-DD"}}
// Result: 1990-01-01 (assuming this was the date of execution)
dateFormat
dateFormat
Returns a String in the specified format representing the date provided.
Syntax
{{dateFormat <date> format=<format> offset=<offset>}}
Parameters
date
: String or Number. A String value parseable as a date, or an Integer representing a UNIX timestamp in milliseconds. See the moment.js docs on parsing for more details.
Options
format
: String. Optional. Tokens representing the date format. Defaults toDD MMMM YYYY
. See the moment.js docs for a list of available date formats.offset
: String. Optional. The offset from UTC, in minutes or hours, to apply to the date before displaying it in the specified format. See the moment.js docs on the utcOffset method for more details.
Returns
A String representing the date provided.
{{dateFormat "2021-05-19" format="DD MMMM YYYY"}}
// Result: '19 May 2019'
{{dateFormat "2021-05-19" format="YYYY-MM-DDTHH:mm:ssZ" offset=2}}
// Result: '2021-05-19T02:00:00+02:00'
{{dateFormat "2021-05-19" format="YYYY-MM-DDTHH:mm:ssZ" offset=-2}}
// Result: '2021-05-18T22:00:00-02:00'
{{dateFormat 1621382400000 format="YYYY-MM-DDTHH:mm:ssZ"}}
// Result: '2021-05-19T00:00:00+00:00'
{{dateFormat 1621382400000 format="YYYY-MM-DDTHH:mm:ssZ" offset=2}}
// Result: '2021-05-19T02:00:00+02:00'
{{dateFormat 1621382400000 format="YYYY-MM-DDTHH:mm:ssZ" offset=-2}}
// Result: '2021-05-18T22:00:00-02:00'
dateToOrdinalString
dateToOrdinalString
Returns the day of the month as an ordinal string.
Syntax
{{dateToOrdinalString <date>}}
Parameters
date
: String. A string value parseable as a date. See the moment.js docs on parsing for more details.
Returns
A String - the ordinal representation of the day of the month.
Example
{{dateToOrdinalString "2021-05-19"}} // Result: 19th
dateStartOf
dateStartOf
Sets a date to the start of a unit of time.
Note: This helper returns a Unix timestamp. It needs to be used in combination with the dateFormat
helper to render a date in human readable format.
Syntax
{{dateStartOf <date> <unit>=true}}
Parameters
date
: String. A string value parseable as a date. See the moment.js docs on parsing for more details.
Options
unit
. Optional. The unit of time. One of[hour, day, week, month, year]
. If omitted, the date provided will be returned unchanged. Note: This is the name of the option, not a String, so quotation marks must be omitted.
Returns
An Integer representing a Unix timestamp in milliseconds.
{{dateStartOf "2021-05-19T01:58:11"}}
// Result: 1621389491000
{{dateStartOf "2021-05-19T01:58:11" day=true}}
// Result: 1621382400000
{{dateStartOf "2021-05-19T01:58:11" year=true}}
// Result: 1609459200000
dateEndOf
dateEndOf
Sets a date to the end of a unit of time.
Note: This helper returns a Unix timestamp. It needs to be used in combination with the dateFormat
helper to render a date in human readable format.
Syntax
{{dateEndOf <date> <unit>=true}}
Parameters
date
: String. A string value parseable as a date. See the moment.js docs on parsing for more details.
Options
unit
. Optional. The unit of time. One of[hour, day, week, month, year]
. If omitted, the date provided will be returned unchanged. Note: This is the name of the option, not a String, so quotation marks must be omitted.
Returns
An Integer representing a Unix timestamp in milliseconds.
{{dateEndOf "2021-05-19T01:58:11"}}
// Result: 1621389491000
{{dateEndOf "2021-05-19T01:58:11" day=true}}
// Result: 1621468799999
{{dateEndOf "2021-05-19T01:58:11" year=true}}
// Result: 1640995199999
dateAdd
dateAdd
Adds the specified duration of time to the date provided.
Note: This helper returns a Unix timestamp. It needs to be used in combination with the dateFormat
helper to render a date in human readable format.
Syntax
{{dateAdd <date> years=<years> months=<months> days=<days>}}
Parameters
date
: String. A string value parseable as a date. See the moment.js docs on parsing for more details.
Options
years
: Integer. Optional. The number of years to add to the date.months
: Integer. Optional. The number of months to add to the date.days
: Integer. Optional. The number of days to add to the date.
If no options are specified, the date will be returned unchanged.
Returns
An Integer representing a Unix timestamp in milliseconds.
Example
{{dateAdd "2021-05-19"}}
// Result: 1621382400000
{{dateAdd "2021-05-19" days=7}}
// Result: 1621987200000
{{dateAdd "2021-05-19" years=1 months=2 days=7}}
// Result: 1658793600000
dateDifference
dateDifference
Calculates the difference between two dates as a duration in the specified unit of measurement.
Note: The result will be truncated (rounded down) to the nearest integer. The second date will be subtracted from the first. Read more in the moment.js docs on the moment#diff
method.
Syntax
{{dateDifference <date1> <date2> measurement=<unit>}}
Parameters
date1
: String or Number. A string value parseable as a date, or a number representing a UNIX timestamp in milliseconds. See the moment.js docs on parsing for more details.date2
: String or Number. A string value parseable as a date, or a number representing a UNIX timestamp in milliseconds. See the moment.js docs on parsing for more details.
Options
unit
. String. Optional. The unit of time in which to measure the difference. One of["years", "months", "weeks", "days", "hours", "minutes", "seconds"]
. Defaults to"days"
.
Returns
An Integer representing a duration of time in the specified units.
Example
{{dateDifference "2021-05-19" "2021-04-19"}}
// Result: 30
{{dateDifference "2021-04-19" "2021-05-19"}}
// Result: -30
{{dateDifference "2021-05-19" "2021-04-01" measurement="months"}}
// Result: 1
{{dateDifference 1621987200000 1621382400000 measurement="days"}}
// Result: 7
dateBetween
dateBetween
Checks whether a date falls within a specified period. Read more in the moment.js docs for the moment#isBetween
method.
Syntax
{{dateBetween <date1> <date2> <date3> granularity=<unit> includeStart=<includeStart> includeEnd=<includeEnd>}}
Parameters
date1
: String or Number. A string value parseable as a date, or a number representing a UNIX timestamp in milliseconds. See the moment.js docs on parsing for more details. This is the date that will be checked.date2
: String or Number. A string value parseable as a date, or a number representing a UNIX timestamp in milliseconds. See the moment.js docs on parsing for more details. This represents the start of the period into which the first date must fall.date3
: String or Number. A string value parseable as a date, or a number representing a UNIX timestamp in milliseconds. See the moment.js docs on parsing for more details. This represents the end of the period in which the first date must fall.
Options
unit
. String. Optional. The granularity at which to perform the check. For example, if"year"
is specified, only the year of the three dates will be taken into account, and more granular units of time will be disregarded. One of["year", "month", "week", "isoWeek", "day", "hour", "minute", and "second"]
. By default the comparison will be evaluated at the most granular (millisecond) level.includeStart
. Boolean. Optional. Whether the period should include the start date. Defaults tofalse
.includeEnd
. Boolean. Optional. Whether the period should include the end date. Defaults tofalse
.
Returns
A Boolean that evaluates to true
if date2 < date1 < date3
, and false
otherwise.
Example
{{dateBetween "2021-04-19" "2021-05-19" "2021-06-19"}}
// Result: false
{{dateBetween "2021-05-19" "2021-05-19" "2021-06-19"}}
// Result: false
{{dateBetween "2021-05-19" "2021-05-01" "2021-05-30"}}
// Result: true
{{dateBetween 1621987200000 1621987100000 1621987300000}}
// Result: true
{{dateBetween "2021-05-19" "2021-05-19" "2021-06-19" includeStart=true}}
// Result: true
{{dateBetween "2021-05-19" "2021-05-01" "2021-05-30" granularity="month"}}
// Result: false
{{dateBetween "2021-05-19" "2021-05-01" "2021-05-30" granularity="month" includeStart=true includeEnd=true}}
// Result: true
Text helpers
toStartCase
toStartCase
Formats the first letter of each word in uppercase. The characters -
and _
are replaced by spaces, and starting and trailing white space is trimmed. Words in camelCase
are separated.
This helper is useful for formatting names, proper nouns and snake_case
values. See the documentation of the corresponding Lodash function for more details.
Syntax
{{toStartCase <text>}}
Parameters
text
: String.
Returns
A String.
Example
{{toStartCase "erlich bachman"}}
// Result: Erlich Bachman
{{toStartCase "erlich bachman"}}
// Result: Erlich Bachman
{{toStartCase "_erlich-bachman_"}}
// Result: Erlich Bachman
{{toStartCase "erlichBachman"}}
// Result: Erlich Bachman
{{toStartCase "ERLICH BACHMAN"}}
// Result: ERLICH BACHMAN
maskAccountNumber
maskAccountNumber
Replaces all the characters of a string, except the last four, with X
. This is useful for hiding sensitive account or other information.
Syntax
{{maskAccountNumber <text>}}
Parameters
text
: String.
Returns
A String.
Example
{{maskAccountNumber "1234567890"}}
// Result: XXXXXX7890
{{maskAccountNumber "1234"}}
// Result: 1234
getInitials
getInitials
Returns the first character of each word in a string in uppercase.
Syntax
{{getInitials <text>}}
Parameters
text
: String. One or more words or names.
Returns
A String.
Example
{{getInitials "erlich edward bachman"}}
// Result: EEB
Policy document helpers
trackedDocumentLink
trackedDocumentLink
Returns a shortened URL that a customer can use to access a a policy document PDF. The document is encrypted with the policyholder's ID number. Interaction with the link is recorded on the notification, with the effect that when the link is clicked, the notification is marked as opened
. The UUID of the document accessed is recorded as part of the notification status update.
Syntax
{{trackedDocumentLink <attachmentId>}}
Parameters
attachmentId
: String. The file UUID, as recorded on the policy object. The following policy documents are supported.
Policy document | Path | Description |
---|---|---|
Policy schedule | policy.schedule_file_id | All policies on Root have a policy schedule. Read more in the Policy documents guide. |
Policy terms | policy.terms_file_id | All policies on Root have a terms file. Read more in the Policy documents guide. |
Welcome letter | policy.policy_welcome_letter_file_id | Policies only have a welcome letter if this is enabled in the product module settings. Read more in the Policy documents guide. |
Policy certificate | policy.policy_certificate_file_id | Policies only have a certificate if this is enabled in the product module settings. Read more in the Policy documents guide. |
Policy anniversary letter | policy.policy_anniversary_file_id | Policies only have an anniversary letter if this is enabled in the product module settings. Read more in the Policy documents guide. |
Quote Summary (only available on application events) | quote_pdf_id | Read more in the Policy documents guide. |
Returns
A String.
Example
{{trackedDocumentLink policy.schedule_file_id}}
// Result: https://cov.is/QHfpawzrw6JlL7lCtJ3qzI
Conditional helpers
Conditional helpers are block helpers that render a block of text depending on the specified merge variable values. The block of text is wrapped in an opening expression denoted by the #
(for example {{#if ...}}
) and a matching closing expression denoted by /
(for example {{/if}}
).
Conditional helpers can optionally include an {{else}}
expression to render one of two blocks of text in the alternative (one block if the expression evaluates to true
, and the other block if the expression evaluates to false
).
This is best illustrated using an example. This is a snippet from the merge variables:
{
"policy": {
"module": {
"includes_secondary_member": false
}
}
}
This is a snippet of the policy schedule HTML template:
{{#if policy.module.includes_secondary_member "==" true}}
<p>This policy covers a secondary member</p>
{{else}}
<p>This policy only covers the main member</p>
{{/if}}
This template will be executed like this:
<p>This policy only covers the main member</p>
Multiple conditional helpers can be nested, as in the example below.
{{#ifIsDefined expression1}}
<p>Will render if expression1 is truthy</p>
{{else}}
{{#ifIsDefined expression2}}
<p>Will render if expression1 is falsy and expression2 is truthy</p>
{{else}}
<p>Will render if both expression1 and expression2 are falsy</p>
{{/ifIsDefined}}
{{/ifIsDefined}}
Nested conditional helpers can also be used, as in the example below.
#if
#if
Used to implement a specified comparison or logical operator. This helper provides the most flexibility in implementing conditional rendering. This helper can be nested to create complex logic, as illustrated in the comparison and logical operators example.
if
can also be used as a simple helper which outputs a single Boolean value, in which case the #
is omitted. The below example illustrates both of these ideas.
{
"policy": {
"module": {
"type": "root_funeral"
},
"status": "active"
}
}
{{if
(if policy.module.type "===" "root_funeral") "&&"
(if
(if policy.status "===" "active") "||"
(if policy.status "===" "pending_initial_payment")
)
}}
==> Result: true
Note: Line breaks within a single handlebars expression, as in the example above, are not allowed where the handlebars are injected as a string value in a JSON file, such as claims blocks. For an example of how to include handlebars expressions in claims blocks, see the claims blocks overview guide.
Syntax
{{#if <value1> <operator> <value2>}}<true block>{{else}}<false block>{{/if}}
Parameters
value1
: Any. The first value to compare.operator
: string. The comparison or logical operator to apply. One of["==", "===", "!=", "!==", "<", "<=", ">", ">=", "&&", "||"]
.value2
: Any. The second value to compare.
Returns
true block
if the operator applied to the two arguments evaluates to true
, and false block
otherwise.
Example
{{#if 1 "===" 2}}True{{else}}False{{/if}}
// Result: False
{{#if 1 "<=" 2}}True{{else}}False{{/if}}
// Result: True
{{#if true "&&" false}}True{{else}}False{{/if}}
// Result: False
{{#if true "||" false}}True{{else}}False{{/if}}
// Result: True
{{#if true "||" (if 1 "===" 2)}}True{{else}}False{{/if}}
// Result: True
#if
has non-standard behaviour on RootOn Root,
#if
does not follow the standard built-in usage as set out in the handlebars.js docs.
#ifEquals
#ifEquals
Checks for strict equality between two values.
Syntax
{{#ifEquals <value1> <value2>}}<true block>{{else}}<false block>{{/ifEquals}}
Parameters
value1
: Any. The first value to compare.value2
: Any. The second value to compare.
Returns
true block
if value1 === value2
, and false block
otherwise.
Example
{{#ifEquals "name" "name"}}True{{else}}False{{/ifEquals}}
// Result: true
{{#ifEquals 3 "3"}}True{{else}}False{{/ifEquals}}
// Result: false
#ifNotEquals
#ifNotEquals
Checks for strict inequality between two values.
Syntax
{{#ifNotEquals <value1> <value2>}}<true block>{{else}}<false block>{{/ifNotEquals}}
Parameters
value1
: Any. The first value to compare.value2
: Any. The second value to compare.
Returns
true block
if value1 !== value2
, and false block
otherwise.
Example
{{#ifNotEquals "name" "name"}}True{{else}}False{{/ifNotEquals}}
// Result: False
{{#ifNotEquals 3 "3"}}True{{else}}False{{/ifNotEquals}}
// Result: True
#ifIsDefined
#ifIsDefined
Checks whether a value is truthy.
Syntax
{{#ifIsDefined <value>}}<true block>{{else}}<false block>{{/ifIsDefined}}
Parameters
value
: Any. The value the helper will cast to a Boolean.
Returns
true block
if !!value === true
, and false block
otherwise.
Example
{{#ifIsDefined "name"}}True{{else}}False{{/ifIsDefined}}
// Result: True
{{#ifIsDefined []}}True{{else}}False{{/ifIsDefined}}
// Result: True
{{#ifIsDefined ""}}True{{else}}False{{/ifIsDefined}}
// Result: False
{{#ifIsDefined null}}True{{else}}False{{/ifIsDefined}}
// Result: False
{{#ifIsDefined 0}}True{{else}}False{{/ifIsDefined}}
// Result: False
{{#ifIsDefined undefined}}True{{else}}False{{/ifIsDefined}}
// Result: False
#ifIsNotDefined
#ifIsNotDefined
Checks whether a value is falsy.
Note: Despite 0
being a falsy value, this helper treats 0
as if it were truthy.
Syntax
{{#ifIsNotDefined <value>}}<true block>{{else}}<false block>{{/ifIsNotDefined}}
Parameters
value
: Any. The value the helper will evaluate as an inverted Boolean.
Returns
true block
if !value === true && value !== 0
, and false block
otherwise.
Example
{{#ifIsNotDefined "name"}}True{{else}}False{{/ifIsNotDefined}}
// Result: False
{{#ifIsNotDefined []}}True{{else}}False{{/ifIsNotDefined}}
// Result: False
{{#ifIsNotDefined ""}}True{{else}}False{{/ifIsNotDefined}}
// Result: True
{{#ifIsNotDefined null}}True{{else}}False{{/ifIsNotDefined}}
// Result: True
{{#ifIsNotDefined 0}}True{{else}}False{{/ifIsNotDefined}}
// Result: False
{{#ifIsNotDefined undefined}}True{{else}}False{{/ifIsNotDefined}}
// Result: True
Context helpers
Context helpers change the context in which the handlebars expressions are evaluated within the merge variable data.
#each
#each
This helper is useful for iterating over an array in the merge variables. It uses an opening expression {{#each ... }}
and a closing expression {{/each}}
to wrap a block of text. The enclosed block of text is then repeated for each item in the array referenced by the opening expression. Read more about #each
in the handlebars.js docs.
An array in the merge variables could look like this:
{
"policy": {
"module": {
"members": [
{
"first_name": "Erlich",
"last_name": "Bachman"
},
{
"first_name": "Richard",
"last_name": "Hendricks"
}
]
}
}
}
An #each
block can be defined to repeat the same block of text for each member. Within the #each
block, any handlebars expressions are evaluated using the current array element as the context. This means the first name of each member is referenced simply as {{first_name}}
, instead of {{policy.module.member.[0].firstname}}
, because these expressions are evaluated in the context of policy.module.member.[<index>].
.
<ul>
{{#each policy.module.members}}
<li>{{first_name}} {{last_name}}</li>
{{/each}}
</ul>
This will result in the following text being executed.
<ul>
<li>Erlich Bachman</li>
<li>Richard Hendricks</li>
<li>Jared Dunn</li>
</ul>
The current array element can be referenced in handlebars using the keyword this
. To access the top-level parent context from within the #each
block, use ../
. To inject the index of the current array element, use the keyword @index
.
{
"policy": {
"start_date": "2021-07-01",
"module": {
"children_included": true,
"children": ["erlich bachman", "richard hendricks"]
}
}
}
{{#if policy.module.children_included "==" true}}
{{#each policy.module.children}}
<tr>
<td>{{add @index 1}}</td>
<td>{{toStartCase this}}</td>
<td>{{dateFormat ../policy.start_date}}</td>
</tr>
{{/each}}
{{else}}
<tr>
<td colspan="3">No children included</td>
</tr>
{{/if}}
Based on the provided merge variables, the template snippet above will execute the following text output.
<tr>
<td>1</td>
<td>Erlich Bachman</td>
<td>01 July 2021</td>
</tr>
<tr>
<td>2</td>
<td>Richard Hendricks</td>
<td>01 July 2021</td>
</tr>
#with
#with
This helper is useful for changing the context within the input object in which handlebars expressions are evaluated. For example, by changing the context to a child object, this helper makes it more efficient to reference the paths of deeply nested properties. Read more about #with
in the handlebars.js docs.
{
"policy": {
"module": {
"member": {
"first_name": "Ehrlich",
"last_name": "Bachmann"
}
}
}
}
<p>Hello {{policy.module.member.first_name}} {{policy.module.member.last_name}}!</p>
The example snippet above can be simplified using the #with
helper like this:
{{#with policy.module.member}}
<p>Hello {{first_name}} {{last_name}}!</p>
{{/with}}
This results in the execution of following text.
<p>Hello Ehrlich Bachmann!</p>
Similar to #each
, the top-level parent context can be accessed from within the #with
block using ../
.
Updated about 2 months ago