πŸ‘¨β€πŸ’»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!).

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

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

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.

  • -!: not equal to.

  • <: less than.

  • >: greater than.

  • >=: greater than or equal to.

  • <=: less than or equal to.

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

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.

Logical operators

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

Flow control

Flow control refers to conditional statements that control the flow, such asif,elseif,else,foretc. 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.

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

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

Operator / method
Syntax example
Result / function
Instructions

~

{{ 'Hello, ' ~ name }}

Hello, Eason

String concatenation operator, will automatically convert variables to strings, even if they are null

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:

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

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

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:

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

Last updated