1. Home
  2. Samba Enterprise
  3. Functions for Handlebars.js
  1. Home
  2. User Guides
  3. Flow Campaigns
  4. Functions for Handlebars.js
  1. Home
  2. Extra
  3. Functions for Handlebars.js

Functions for Handlebars.js

This post is also available in: Czech Polish

Below you can find a list of all available basic functions and also helpers that can be used when constructing the request body in the Webhook node definition in Flow campaigns. Handlebars.js is used as the templating language.

To test the correct syntax, you can directly use the test interface in the Webhook definition in Samba or the demo from Handlebars.js.

Basic functionality

if

It is used to create conditions. If the argument returns false, undefined, null, "", 0, or [], the block is not drawn.

Example

{{# if true}}
hello
{{/ if}}
{{# if false}}
bye
{{/ if}}

is evaluated as

hello

unless

It is used as the opposite of the if function – the block is drawn only if the condition is not met.

Example 1

{{# unless true}}
hello
{{/ unless}}
{{# unless false}}
bye
{{/ unless}}

is evaluated as

bye

Example 2

{{# each customers}}
{{# unless @first }}
{{/ unless}}
{
...
}
{{/ each}}

inserts a newline character before the JSON with every customer except the first.

Example 3

{{# each customers}}
{
...
}
{{# unless @last }},{{/ unless}}
{{/ each}}

inserts a comma after the JSON with every customer except the last one.

each

It is used to iterate over a list.

Example 1

{{# each customers}}
...
{{/ each}}

iterates over the list of customers that have entered the Webhook node. This is the basic construct for creating a request in a Webhook node.

Example 2

"parameters": [
    {{~# each parameters }}
    {
      "name":  {{ json-string @key }},
      "value": {{ json-string . }}
    }{{# unless @last }},{{/ unless }}{{/ each }}
  ]

writes all custom parameters to the object array.

with

This helper can be used to evaluate a part of a block only if the variable exists and change the evaluation context at the same time. To handle the case where the variable does not exist, the {{else}} section can be used.

Optional attributes

This function should be used when rendering all optional attributes, which are usually all but the customer ID.

Example 1 

...
"lastname": "{{ lastName }}"{{# with email }},
"email": "{{ . }}"{{/ with }},
"registration": {{ registeredOn }},
...

Here, the email address is inserted into JSON only if it is specified by the customer.

Example 2

...
"lastname": "{{ lastName }}",{{# with email }}
"email": "{{ . }}" {{else}}
"email": "unknown" {{/ with }},
"registration": {{ registeredOn }},
...

In this case, at least the value “unknown” is always inserted if the email address does not exist.

Example 3

...
"lastname": "{{ lastName }}",{{# with parameters.bonus_points }}
"points": "{{ . }}" {{else}}
"points": "0" {{/ with }},
"registration": {{ registeredOn }},
...

In this case, if the bonus_points custom parameter does not exist, a value of at least 0 is always inserted.

lookup

This function can be used to dive into the path of the selected parameter. This is especially useful for parameters whose names need to be escaped and so the standard dot notation cannot be used.

Example

...
 "bonus_points": "{{ lookup parameters "Bonus body" }}",
...

Takto lze funkci použít pro vložení vlastního zákaznického parametru jménem Bonus body do těla volání API.

In this way, the function can be used to insert a custom customer parameter named Bonus body into the body of the API call.

and, or

Basic logical operators.

Example

{{ and (is-null a) (is-null b) (is-null c) }}

not

It is used to negate the truth value.

Example

{{ not (is-null a) }}

eq, neq, gt, gte, lt, lte

It is used to compare values.

Example

{{ eq (is-null a) (is-null b) }}

dot notation

It is used to dive into the selected path of an attribute whose name does not conflict with the supported format and does not need to be escaped. For unconventional parameter names, the lookup function should be used.

Example

Finding the Bonus_body parameter in the parameters object.

...
"bonus_points": "{{ parameters.Bonus_body }}",
...

tilde ~

Using a tilde inside a handlebar removes all white characters before or after the handlebar.

Example 1

{{~ let "today" "2023-01-05" }}

removes all white characters before this handlebar.

Example 2

{{~ let "today" "2023-01-05" ~}}

removes all white characters before and after this handlebar.

Auxiliary functions

let

This function can be used to define custom variables.

Example

{{ let "today" "2023-01-05" }}

This way you can define a variable “today” at the beginning of the request body, which can then be used as a handlebar:

"today_is": "{{today}}"

is-null

The function returns true or false, typically used when creating conditions.

Example

...
"email": {{#if (is-null email)}}"email not given"{{/if}}
...

In this way, for all customers without an email address, you can include the selected replacement text in the API call.

to-upper

Change all letters to uppercase.

Example

{{to-upper "hello"}}

returns

"HELLO"

to-lower

It changes all the letters to lowercase.

Example

{{to-lower "HELLO"}}

returns

"hello"

capitalise/capitalize

Change the first letter of each word to uppercase.

Example

{{capitalise "hello world"}}

returns

"Hello World"

md5

Creates a hash using the MD5 algorithm.

Example

{{md5 "hello world"}}

returns

5eb63bbbe01eeed093cb22bb8f5acdc3

sha256

Creates a hash using the SHA-256 algorithm.

Example

{{sha256 "hello world" encoding="base64url"}}

returns

uU0nuZNNPgilLlLX2n2r-sSE7-N6U4DukIj3rOLvzek=

base64

Creates a hash using the base64 algorithm.

Example

{{base64 "hello world"}}

returns

aGVsbG8gd29ybGQ=

base64url

Encoding using the base64url algorithm.

Example

{{base64url "hello world"}}

returns

aGVsbG8gd29ybGQ=

date-format

Converts timestamp (ms) to the selected date format in YYYY-MM-ddTHH:mm:ss.sTZD (more information).

Example

...
"registrationDate": {{ json-string (date-format registeredOn format="YYYY-MM-dd") }},
"registrationTime": {{ json-string (date-format registeredOn format="HH:mm:ss") }},
"registrationDateTime": {{ json-string (date-format registeredOn format="YYYY-MM-dd HH:mm:ss") }},
...

date-parse

Converts date in format YYYY-MM-ddTHH:mm:ss.sTZD (more information) to timestamp (ms).

Example

"registrationDate_timestamp": {{ json-string (date-parse registeredOn format="YYYY-MM-dd") }}

silent

This function causes the content inside the block to be ignored. It is useful for commenting during testing.

Example

{{#silent}}
this will be never rendered
{{/silent}}

json-string

It interprets the text as a JSON string and encodes it accordingly. This can be useful when, for example, a variable may contain quotes.

Example

"customer_id": {{ json-string id }} 

is a safer version of this notation

"customer_id": "{{ id }}" 

regexp

Used to create conditions using regular expressions.

filter

It is useful, for example, for listing all customer parameters that meet a certain condition.

Example

"parameters": [
{{~# each (filter (not is-date-param) parameters) }}
{
"name": {{ json-string @key }},
"value": {{ json-string . }}
}{{# unless @last }},{{/ unless }}
{{~/ each }}
]

now

Returns the current timestamp in milliseconds. The value is generated at the moment of batch creation, i.e. in case of multiple occurrences at individual customers it will have the same value within 1 batch.

You can use this variable to create a hashed signature by helper sha256 from timestamp and password.

Example

{{ now }}

{{ date-format (now) format="YYYY-MM-dd HH:mm:ss" timezone="Europe/Prague" }}

uuid

Returns a random UUID for any occurrence of the given variable, i.e. it can be used to identify the whole batch (if used only once in the header), or for individual customers (if inserted into the request body). If for some reason a given batch is processed again in Samba, it is not guaranteed that the UUIDs will be the same. If you need a Microsoft GUID, you can use the to-upper helper.

Versions

  • version=”3″
    • UUID is always the same using the string parameter (i.e. it’s not randomly generated)
  • version=”4″
    • default
    • UUID is always random

Example

{{ uuid }}

{{ uuid customers.[0].email version="3" }}

This post is also available in: Czech Polish

Updated on February 15, 2023

Was this article helpful?

Related Articles