For the complete documentation index, see llms.txt. This page is also available as Markdown.

Handlebars

Handlebars is a JavaScript library used for creating dynamic templates.

It allows you to define templates with placeholders, and these placeholders are replaced with data when the template is triggered with the information sent in the payload.

Handlebars within Sortment support features like conditional operations, loops, date time formatting, partial searches, and custom helpers, making it a very cool tool to use when creating your templates.

Handlebars in Sortment's Templates

To understand how handlebars work, we first need to establish a sample payload, based off which the Handlebars function and applicability can be demonstrated.

{
  "members": [
    {
      "first_name": "John",
      "last_name": "Doe",
      "city": ""
    }
  ],
  "investments": [
    {
      "amount": 10,
      "company": "L&T"
    },
    {
      "amount": 20
    }
  ],
  "companies": {
    "L&T": "India",
    "Apple": "USA"
  },
  "user_type": "pro",
  "period": {
    "start": "2023-10-01",
    "end": "2023-10-03"
  },
  "classification": ["active", "pro"]
}

Sortment has default as well as custom handlebars that you can use in your templates. Let’s have a look at what these are and how to use them using the above payload and data as example responses.

To access placeholder values
To access values of an object
To access object values from an array
To access values from an array
To get length of an array
To check if variable exists
Approach 1: To check if variable equals a certain value
Approach 2: To check if variable equals a certain value
To check if multiple variables equal a certain value (AND and OR options)
To check if variable equals a regular expression
To check if string starts with certain characters
To use OR operator between two variables
To use AND operator between two variables
To use a specified default value if the actual value does not exist (or is null)
To sum two numbers
To subtract two numbers
To multiply two numbers
To divide numbers
To perform modulo
To round numbers
To check for negative condition (show something only if key doesn't exist)
To iterate over a list/array
To use combination of conditions and nesting (advanced)
To lookup values and use them in placeholders (advanced)
To split variables and get value at index
To split variables and loop on it
To check if timestamp equals to today or yesterday or tomorrow
To check difference between two dates (in seconds, minutes, days, hours, month, hours, etc) The date and time must be in RFC-3339, ISO_8601 or the helper will return NaN
To format date value
To format datetime value
To format numeric value
To trim spaces from value
To trim values to a particular length
To trim value to a particular length and then truncate until the last occurrence of the specified delimiter
To remove a string
To sum multiple numbers
Current time with date format
Specific time with formatting and timezone

Last updated