Principles of templating

This is the Directual syntax for dealing with object fields 📜

For dealing with data in scenarios and filters you can use Templating. Directual Templating system is based on {{ mustache }} library. But there are some useful add-ons to the basic technology.

Directual Academy

Find all the details in the Academy tutorial: Template system

Basic templating principle

For using the value of the field we are typing its system name in double curly braces: {{field_system_name}}. If we want to fetch the property value of the linked object, we type the field-link, then dot, then the system name of the linked object: {{field_type_of_link.linked_object_field}}.

Inputs for templating

There are two types of input fields for templating. The first—select the field:

Example of selecting linked field {{author_id.last_name}}

The second is a textarea field:

Advanced templating techniques

Escaping special characters

{‌{#escape}‌}{‌{name}‌}{‌{/escape}‌}
// if {{name}} is 'aaaa"aaaa', this operator will add
// a backslash before each special character for escaping them.
// The result of templating will be: 'aaaa\"aaaa'

Removing line breaks

{{#stripNewLine}‌}this new
line{‌{/stripNewLine}}
// operator removes line breaks
// result of templating will be: 'this new line'

Escaping line breaks (useful for JSONs)

This operator escapes \n (line breaks). Thanks to that a multiline JSON can be sent in the http-request body as follows:

//{{json}} is a field value with multiline json
//body of the http-request:
{"key":"{{#escape}}{{#escapeNewline}}{{json}}{{/escapeNewline}}{{/escape}}"}

Formatting date

{{#formatDate|dd MMMM, Y}}{{date}}{{/formatDate|dd MMMM, Y}}
// {{date}} == 2020-04-27T12:31:19.000Z
// result of templating will be: 27 April, 2020

Learn more about date/time formats.

Handling with boolean properties

// shows if is_vip == true
{{#is_vip}}vip{{/is_vip}}
// shows if is_vip == false
{{^is_vip}}not vip{{/is_vip}}

Nesting arrays of linked objects

If a property has arrayLink type, we can nest its objects as follows:

// {{books_ids}} — field type of arrayLink
// {{title}} and {{year}} — the fields of linked objects
{{#books_ids}}
– {{title}}, {{year}}
{{/books_ids}}
// If such {{books_ids}} contains, for example, three links,
// such a construction will result as follows:
// – Life of Samuel Johnson, 1791
// - Crime and Punishment, 1866
// - The Sun Also Rises, 1927

Handling arrays

// We can template the Array field, iterationg its elements:
{{#arr}}
- {{.}}
{{/arr}}
// if {{arr}} == a,b,c
// result of templating will be:
// - a
// - b
// - c

Also, we can avoid empty elements of an array:

{{#musketeers}}
{{#.}}
- {{.}}
{{/.}}
{{/musketeers}}
// {{musketeers}} == "Athos", "Aramis", "", "D'Artagnan"
// result of templating will be:
// - Athos
// - Aramis
// - D'Artagnan

Check the tips for advanced working with Arrays.

Handling JSON

Let's a property has json-type, and equals to:

// {{book-json}} is a json-type field:
{
"title":"The Wealth of Nations",
"year": 1776,
"autor: {
"first_name":"Adam",
"last_name":"Smith"
},
"is_classic":true
}

We can address to its properties as follows:

{{book-json.title}} == The Wealth of Nations
{{book-json.is_classic}} == true
{{book-json.autor.first_name}} == Adam

Remember, that if JSON contains array of objects, you have to apply JS SDK methods (here it is an instruction).

Using now, today, +/- days

For using this feature you have to turn on Evaluate as a JS-expression

now == current date and time
today == current date, time 00:00
now -1 days == yesterday
today +N days == N days after today
now +10 minutes +15 seconds == 10 minutes and 15 seconds after the current moment