Many of the templating based deatures in Chakra is based on handlebarsjs. Handlebars templates look like regular text with embedded Handlebars expressions.
Hi {{name}}, a new lead has been assigned to you
A handlebars expression is a {{, some contents, followed by a }}. When the template is executed, these expressions are replaced with values from an input payload.
You can access nested properties from the input json object using the dot notation
Hi {{lead.cutsomer_name}} your order has been booked for {{lead.order_date}}
Sample input object
{
"lead": {
customer_name: "Mohit",
order_date: "2022-08-12"
}
}
Usually the templates will be computed with runtime payload. But some templates allow you to configure a static json object which is inputted to the template along with the runtime payload.
You can use this context to store static data like telephony phone numbers and campaign info.
Handlebars comes with a range of helper functions for creating complex templates
#if helper allows you top write if conditions inside of the tempalate. More info can be found here
Example
{{#if name}}
Dear Sir/Ma'm your order has been placed
{{else}}
Dear {{name}} your order has been placed
{{/if}}
This compiles as following if name is not present
Dear Sir/Ma'm your order has been placed
and as following if name is present
Dear Mohit your order has been placed
lookup allows you to do a value lookup with a dynamic key.
Example 1
We tried reaching out to you. You can reach us at {{lookup context.phonemap}}
where phonemap can be defined in the context field as
{
"phonemap": "080-11223344"
}
Example 2
(nested if lookup)
We tried reaching out to you. You can reach us at {{#if lookup context.phonemap city}} {{lookup context.phonemap city}} {{/if}}
where phonemap can be defined in the context field as
{
"phonemap": {
"bangalore": "080-1122334455",
"hyderabad": "040-1122334455",
"pune": "020-1122334455",
}
}
We also provide a set of custom helpers on top of the standard helpers.
access nested properties of an object with support for default
Hi {{get name default="Sir/Mam"}} your order has been placed
This compiles as following if name is not present
Dear Sir/Mam your order has been placed
and as following if name is present
Dear Mohit your order has been placed
fetch the first defined value when provided with a list of paths
Hi {{coalesce name firstname full_name}} your order has been placed
This expression first checks for name, then firstname and then full_name.
formats a date field to readable string
Hi your order will reach on {{formatDate date "YYYY-MM-DD"}}
this will compile to
Hi your order will reach on 2022-09-01
More examples:
dddd, MMMM Do YYYY, h:mm:ss a
- "Sunday, February 14th 2010, 3:25:50 pm"ddd, hA
- "Sun, 3PM"[Today is] dddd
- "Today is Sunday"Complete list of formats can be found here