> For the complete documentation index, see [llms.txt](https://en.help.firstline.cc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://en.help.firstline.cc/feature/other/template-syntax.md).

# Template syntax

## Overview

This article introduces the template syntax; by using variables and expressions, you can easily build the desired content and perform substitutions.

The template syntax can currently be applied to the following features:

* Certain message operations in conversation scripts.
* Parameters (Params) and callbacks in Webhook scripts.

## Variable naming rules

Like most programming languages such as Python and JavaScript, conversation variables must follow the following rules:

1. Variable names must start with a letter (a-z, A-Z) or an underscore (\_).
2. Subsequent characters can be letters, digits (0-9), or underscores (\_).
3. Variable naming is case-sensitive; uppercase letters (A-Z) and lowercase letters (a-z) are considered different characters.

Here are some examples of variable names that comply with the naming rules:

* myVariable
* count
* first\_name
* age123

## Symbols and operators

### Delimiters

The template syntax uses `{{ ... }}`  and  `{% ... %}` two kinds of symbol expressions. The former delimiter will be replaced with the actual string output in the content. As the syntax below shows, it will actually output “王小明，您好！” (Wang Xiaoming, hello!).

```javascript
{{ name }}，您好！
```

You can also use a dot (.) to access deeper parts of a variable, for example

```php
{{ contact.first_name }}

{# You can also access content using square brackets #}
{{ contact['first_name'] }}
```

And the latter  `{% ... %}` delimiter is used for performing logical operations, such as for, if ... etc.

```php
{# Set the variable name to Wang Xiaoming #}
{% set name = '王小明' %}

{# This will display 0,1,2,3 sequentially up to 10 #}
{% for i in range(0, 10) %}
    {{ i }}
{% endfor %}
```

### Arithmetic operators

The template provides simple operators; others include the following

| Operator | Syntax example | Result | Instructions                                                                |
| -------- | -------------- | ------ | --------------------------------------------------------------------------- |
| `+`      | `{{ 1 + 2 }}`  | `3`    | Addition, only applicable to numbers, cannot be used to concatenate strings |
| `-`      | `{{ 5 - 3 }}`  | `2`    | Subtraction                                                                 |
| `*`      | `{{ 4 * 2 }}`  | `8`    | Multiplication                                                              |
| `/`      | `{{ 5 / 2 }}`  | `2.5`  | Division                                                                    |
| `//`     | `{{ 7 // 2 }}` | `3`    | Integer division (quotient)                                                 |
| `%`      | `{{ 11 % 7 }}` | `4`    | Modulus (remainder)                                                         |

### Comparison operators

Can be used directly with `==` ,`!=`, `<`, `>`, `>=`  and `<=`, and will return a boolean value based on the comparison result.

* `==`: equal to.&#x20;
* `-!`: not equal to.
* `<`: less than.
* `>`: greater than.
* `>=`: greater than or equal to.
* `<=`: less than or equal to.

For more complex comparisons, you can use `matches`The matches operator compares using regular expressions.

```php
{% if phone_number matches '/^[\\d\\.]+$/' %}
{% endif %}
```

### Ternary (conditional) operator

This operator accepts two operands as values and one operand as the condition. The syntax is:`condition ? value1 : value2` , below are some simple examples.

```php
{{ name ? 'yes' : 'no' }}

{{ age >= 18 ? 'adult' : 'child' }}
```

### Logical operators

You can use ?? to evaluate OR logic; if `expression1` is present/true, it returns `expression1`; otherwise it returns `expression2`.&#x20;

```php
{# If age is not null, display the value of name; otherwise display unknown #}
{{ name ?? 'unknown' }}
```

## Flow control

Flow control refers to conditional statements that control the flow, such as`if`,`elseif`,`else`,`for`etc. Control syntax should be placed within `{% ... %}` delimiter content.

### if...else

When the condition is met, the statements inside the if block will be executed; when it is not met, another statement will be executed.

```php
{% if age >= 18 %}
    Adult
{% else %}
    Minor
{% endif %}

{# Check whether a value exists #}
{% if users is defined %}
  {{ user.username }}
{% endif %}
```

### for

Repeatedly executes until a specified condition evaluates to false; it stops executing when the condition no longer holds. Within the for block, you can use built-in special variables.\
\
The special variables are as follows:

| Variable name | Description                                       |
| ------------- | ------------------------------------------------- |
| loop.index    | Displays the current index, starting from 1       |
| loop.index0   | Displays the current index, starting from 0       |
| loop.length   | Length of the array                               |
| loop.first    | Returns true if it is the first item of the array |
| loop.last     | Returns true if it is the last item of the array  |

```php
{% for user in users %}
    {{ user.name }}
{% endfor %}

{# You can also use else to handle the case when the array is empty, for example #}
{% for user in users %}
    {{ loop.index }} - {{ user.name }}
{% else %}
    No users have been found.
{% endfor %}
```

Below is a description written for the usage of `split` based on the tone and format you provided; it can be directly appended to the original tutorial content:

***

## Text filters

### Operator

<table><thead><tr><th width="103.56640625">Operator / method</th><th>Syntax example</th><th>Result / function</th><th>Instructions</th></tr></thead><tbody><tr><td><code>~</code></td><td><code>{{ 'Hello, ' ~ name }}</code></td><td><code>Hello, Eason</code></td><td><strong>String concatenation operator</strong>, will automatically convert variables to strings, even if they are <code>null</code></td></tr></tbody></table>

### String split

When you need to split a piece of text into multiple parts based on a delimiter, you can use the `split` feature. For example, to split a name into a list of characters so you can access each character individually:

```php
{{ name|split('') }}

{# If name is “王小明”, executing this will produce: #}
['王', '小', '明']
```

You can also specify a particular delimiter to split by, for example a comma-separated list of color tags:

```php
{{ 'red,blue,green'|split(',') }}

{# The result is: #}
[['red', 'blue', 'green']
```

The split data can be processed one by one using the `for` syntax, commonly used for displaying tags, decomposing text content, and other needs.

## Date filters

The date syntax can accept strings or DateTime objects. For example, to display the current date, pass the word “now” as the parameter to the syntax.

### Display current time

```php
// Output is 2022-05-12 22:21:10
{{ 'now' | date('Y-m-d H:i:s') }
```

### Specify timezone

By default, dates will use the FIRST LINE server’s timezone for display, but you can override it by explicitly specifying a timezone:

```php
// Specify a special timezone conversion
{{ 'now' | date('Y-m-d H:i:s', 'Asia/Tokyo') }}
```

If the date is already a DateTime object and you want to keep its current timezone, pass the timezone value as false.

```php
{{ product.created_at | date('Y-m-d H:i:s', false) }}
```
