# Templating Techniques for Scenarios

{% hint style="info" %}
Turn off **smart field inputs** to use the following techniques
{% endhint %}

### 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}}"}
```

### Replace `"`→`'`

```
{{#replaceDoubleQuotes}}hello "there'{{/replaceDoubleQuotes}}

// result hello 'there'
```

### Encode URI

Executes [encodeUriComponent()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) for the string

```
{{#encodeURIComponent}}Mr. John Smith{{/encodeURIComponent}}

// result: Mr.%20John%20Smith
```

### Decode URI

Decodes the string encoded by `encodeURIComponent`

```
{{#decodeURIComponent}}Mr.%20John%20Smith{{/decodeURIComponent}}

// result: Mr. John Smith
```

### Convert to JS-multiline

Useful feature for applying regex.

```
{{#toJsMultiLine}}test
test{{/toJsMultiLine}}

//Regex example (pay attention – no quotes ""):

{{#toJsMultiLine}}{{template}}{{/toJsMultiLine}}.replace(/\{\$name\}/g,"{{field}}")

// result: test\ntest
```

### Escape JSON

```
{{#escapeJson}}He didn't say, "Stop!"{{/escapeJson}}

// result: He didn't say, \"Stop!\"
```

### Formatting date

{% hint style="info" %}
Hint: You may also use `moment().format()`
{% endhint %}

```
{{#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](/data/data-types/formatting-date-time-data.md).

### Handling 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](/data/data-types.md#available-data-types) type, we can nest its objects as follows:&#x20;

```
// {{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
```

{% embed url="<https://youtu.be/jhiYG6_nK1s>" %}

### 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](/template-system/tips-for-working-with-data.md) with Arrays.

### Handling JSON

#### JSON templating

Let's a property has [json-type](/data/data-types.md), 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](/template-system/tips-for-working-with-data.md)).

#### JSON as a JS object

Also, you can apply JS for parsing JSON, handle it as a js-object (don't forget to [turn on JS-evaluating](/template-system/evaluating-as-js-expression.md))

```
{{json}}.title
// or
{{json}}.title[0].name
// or
{{json}}["title"]
```

#### Lodash for JSON parsing

```javascript
_.get({{json}},"title[0].name")
```

### Using now, today, +/– days

{% hint style="info" %}
For using this feature you have to turn on [Evaluate as a JS-expression](/template-system/evaluating-as-js-expression.md)
{% endhint %}

```
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
```

### Using Lodash

You can apply [Lodash](https://lodash.com/) in expressions. Lodash is a modern JavaScript utility library delivering modularity, performance & extras.

```javascript
// some examples:
_.defaults({ 'a': 1 }, { 'a': 3, 'b': 2 });
// → { 'a': 1, 'b': 2 }
_.partition([1, 2, 3, 4], n => n % 2);
// → [[1, 3], [2, 4]]
_.difference([2, 1], [2, 3]);
// => [1]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://readme.directual.com/template-system/basics-of-template-system/advanced-templating-techniques.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
