How to create your Agent

icon picker
Syntax

General Syntax Information

The syntax can be used in the following
@Slot
s:
@Text
:
@Expression
and
@Expression with Control Structure
can be used in Text List field
@Attachment
:
@Expression
and
@Expression with Control Structure
can be used in Source and Caption fields
@Slot Filling
:
@Expression
and
@Expression with Control Structure
can be used in Question field
@button
:
@Expression
can be used in Label field
@condition
:
@Expression
can be used in Condition field
@Timer
:
@Expression
can be used in If field
@Memory
:
@Expression
and
@Expression with Control Structure
can be used in Context key field
@External Request
:
@Expression
and
@Expression with Control Structure
can be used in URL, Value fields on Headers and Query parameters tabs, Data field, Name field on Response tab
@Incoming Request Slot
:
@Expression
and
@Expression with Control Structure
can be used in Value field
@Synonym Slot
:
@Expression
and
@Expression with Control Structure
can be used in Source field

Syntax of the
@Platform
uses the syntax of the Jinja template engine:
@Template
s in syntax are divided into 2 concepts:
@Expression
. Enclosed in double curly braces.
Examples:
{{ client_message }}
{{ number * 10 }}
{{ chat_id == ‘abcde0123456789’ }}
{{ uuid4() }}
{{ some_variable is defined }}
@Expression with Control Structure
. Enclosed in curly braces with % symbol. ​
Examples:
{% if client_message == ‘hello’ %}Hi{% else %}Bye{% endif %}
{% for number in [1, 2, 3] %}{{ number }}{% endfor %}
Supported data types
Data type
Memory
Examples of output in a text slot
Note
1
Strings
{{ 'string' }}
{{ "string" }}
Empty string: {{ '' }} or {{ "" }}
string
Empty string is displayed as no value
If a string is enclosed in double quotes and there is a double quote character inside the string as a punctuation mark, the quote character inside the string must be escaped with the \ character to pass the
@Script Validation
and display correctly. Example:
**{{ "cinema \"Victory\" " }}**
If a string is enclosed in single quotes and there is a single quote character inside the string as a punctuation mark, the quote character inside the string must be escaped with the \ character to pass the
@Script Validation
and display correctly. Example:
**{{ 'I\'ve made an order' }}**
In cases where a string is enclosed in double quotes and there is a single quote character inside the string, or if a string is enclosed in single quotes and there is a double quote character inside the string, escaping is not required.
2
Numbers
{{ 5 }}
{{ 42.23 }}
**5**
42.23
3
Booleans
{{ true }}
{{ false }}
True
False
4
Null values
{{ None }}
Displayed as no value
5
Objects
{{ {"one": 1, "two": 2, "colors": ["red", "green", "blue"]} }}
**{"one": 1, "two": 2, "colors": ["red", "green", "blue"]}**
are available in the
@Platform
6
Arrays
Numbers: [1, 2, 3, 4, 5]
Strings: ["one", "two", "three"]
Arrays: [["one", "two", "three"], ["cat", "dog", "bird"], ["green", "blue", "red"]]
**[1, 2, 3, 4, 5]** **["one", "two", "three"]** **[['one', 'two', 'three'], ['cat', 'dog', 'bird'], ['green', 'blue', 'red']]**
Accessing array elements is done using a dot (.) Accessing the corresponding number of an array element is done using numbers. Array element numbering starts from zero, so accessing the first element of an array is denoted as 0. Examples: **{{ array.0 }}**
7
Dates
Convert data to date format is possible only with the string_to_time filter
Example: {{ "2000-06-15 18:50:47" | string_to_time("%Y-%m-%d %H:%M:%S") }}
More details:
@Deleted Row
2000-06-15 18:50:47
Time zone is not stored
There are no rows in this table
Reserved methods of objects
Name
Availability on the Platform
Meaning
How to use
Example
1
items
Yes
Returns the key-value pairs of a dictionary.
{{ data.items() }}
{{ data.items() | list }}
Результат: [('a', 1), ('b', 2)]
2
keys
Yes
Returns the dictionary keys
{{ data.keys() }}
{{ data.keys() | list }}
Результат: ['a', 'b']
3
values
Yes
Returns dictionary values
{{ data.values() }}
{{ data.values() | list }}
Результат: [1, 2]
4
clear
No
5
copy
No
6
fromkeys
No
7
get
No
8
pop
No
9
popitem
No
10
setdefault
No
11
update
No
There are no rows in this table
Important: when parsing an
@Incoming Request Slot
, a response to an
@External Request
or a
@Notification
, access to the object key, if its name matches the name of the , occurs through square brackets and quotes. ​Example: {{ data["keys"] }}

Expressions

Literals
8
Operator
Designation
Application area
How to use
Example
1
Line
"string"
String declaration
Declaration of an empty string
String declaration
Пустая строка
{{ '' }} или {{ "" }}
Непустая строка
{{ 'str' }} или {{ "str" }}
Accessing string characters
str == {{ 'string' }}
{{ str[0] }} = s
{{ str[1] }} = t
...
Line slice
str == {{ 'string' }}
{{ str[:2] }} = st
{{ str[2:] }} = ring
{{ str[2:5] }} = rin
{{ 'Hello World' }}
Result: Hello World.
{{ "Hello World" }}
Result: Hello World.
{{ 'Hello World'[1] }}
Result: e.
{{ 'Hello World'[:5] }}
Result: Hello.
{{ 'Hello World'[5:] }}
Result: World.
2
Integer
42
123_456
Declaring an Integer
{{ num }}
{{ 123 }}
Result: 123.
{{ 1_2_3 }}
Result: 123.
3
Number with a dot
42.23
42.1e2
123_456.789
Declaring a real number
{{ num }}
{{ 42.23 }}
Result: 42.23.
{{ 42.1e2 }}
Result: 4210.0.
{{ 4_2.23 }}
Result: 42.23.
4
Lists
['list', 'of', 'objects']
Declaring a list with arbitrary object types
Accessing an element from a list
Declaring a list
Пустой список
{{ [] }}
Список чисел
{{ [num1, num2, ...] }}
Список строк
{{ ['str1','str2', ...] }}
{{ ["str1","str2", ...] }}
Accessing list elements
list = {{ [A, B, ...] }}
{{ list[0] }} = A
{{ list[1] }} = B
...
{{ [1, 2, 3] }}
Result: [1, 2, 3].
{{ ['one', 'two'] }}
Result: ['one', 'two'].
{{ ['one', 'two'] }}[0]
Result: one.
5
Dictionaries
{'dict': 'of', 'key': 'and', 'value': 'pairs'}
Dictionary declaration
Accessing an element from a dictionary
Dictionary declaration
{{ { key1:val1, key2:val2, ... } }}
Accessing Dictionary Items
dict = {{ { key1:val1, key2:val2, ... } }}
{{ dict[key1] }} = val1
{{ dict[key2] }} = val2
...
{{ {1:'A', 2:'B', 3:'C'} }}
Result: {1: 'A', 2: 'B', 3: 'C'}.
{{ {1:'A', 2:'B', 3:'C'}[1] }}
Result: A.
{{ {'one': 'A', 'two': 'B', 'three': 'C'} }}
Result: {'one': 'A', 'two': 'B', 'three': 'C'}.
{{ {'one': 'A', 'two': 'B', 'three': 'C'}['three'] }}
Result: C.
6
Arrays
{{ [{'id': 3, 'name': 'O'}, {'id': 1, 'name': 'E'}, {'id': 2, 'name': 'S'}] }}
Declaring a data array
Accessing an element from an array
Array Declaration
{{ [{'id': id1, 'value': val1 }, {'id': id2, 'value': val2 }, ...] }}
Accessing Array Elements
array = {{ [{'id': id1, 'value': val1 }, {'id': id2, 'value': val2 }, ...] }}
{{ array[0] }} = {'id': id1, 'value': val1 }
...
{{ [{'id': 3, 'name': 'O'}, {'id': 1, 'name': 'E'}, {'id': 2, 'name': 'S'}] }}
Result: [{'id': 3, 'name': 'O'}, {'id': 1, 'name': 'E'}, {'id': 2, 'name': 'S'}].
{{ [{'id': 3, 'name': 'O'}, {'id': 1, 'name': 'E'}, {'id': 2, 'name': 'S'}] [0] }}
Result: {'id': 3, 'name': 'O'}.
7
Boolean values
true
false
Expressions and Boolean Variables
{{ true }}
{{ false }}
{{ А == true }}
Result: True if the boolean variable A is true.
{{ (A == B) == true }}
Result: True if the expression (A == B) is true.
No results from filter
Mathematical Expressions
8
Operator
Designation
Application area
How to use
Example
1
Addition
+
Addition of integers and real numbers
String concatenation
Combining array elements into one array
{{ num1 + num2 + ... }}
{{ 'str1' + 'str2' + ... }}
{{ "str1" + "str2" + ... }}
{{ mas1 + mas2 + ... }}

{{ 1 + 2 + 3 }}
Result: 6.
{{ 'a' + 'b' + 'c' }}
Result: abc.
{{ 0.5 + 1.5 }}
Result: 2.0.
{{ [0,1,2] + [3,4,5] }}
Result: [0, 1, 2, 3, 4, 5].
2
Subtraction
-
Subtracting integers and real numbers
{{ num1 - num2 - num3 - ... }}
{{ 4 - 2 - 1 }}
Result: 1.
{{ 0.5 - 1.5 }}
Result: -1.0.
3
Division
/
Division of integers and real numbers
{{ num1 / num2 / num3 / ... }}
{{ 1 / 2 }}
Result: 0.5.
{{ -1.5 / 0.5 }}
Result: -3.0.
{{ 6 / 3 / 2 }}
Result: 1.0.
4
Division that will return only the integer part of the result
//
Division of integers and real numbers
Restrictions: ​The // operator returns an integer only when dividing integers. If one of the operands is a real number, then the result will also be a real number.
{{ num1 // num2 // num3 // ... }}
{{ 6 // 3 // 2 }}
Result: 1.
{{ 1.5 // 0.5 }}
Result: 3.0.
5
Calculating the remainder of division
%
Calculating the remainder of division of integers and real numbers
{{ num1 % num2 }}
{{ 6 % 4 }}
Result: 2.
{{ 1.5 % 0.5 }}
Result: 0.0.
6
Multiplication
*
Multiplying integers and real numbers
{{ num1 * num2 * num3 * ... }}
{{ -1.5 * -0.5 }}
Result: 0.75.
{{ -2 * 3 * 4 }}
Result: -24.
No results from filter
Restrictions:
The maximum number (modulo) that can be used in arithmetic operations is 4294967296. If you use large numbers, there will be an error (False will be stored in memory as a variable, for example).
If you add strings/arrays, the maximum allowed length of a string/array is 1000. When attempting to add/concatenate strings/arrays that produce results longer than 1000 characters, the value “Addition of objects with length greater than 1000 is not allowed” is written to the
@error
@Context Variable
.
To concatenate strings/arrays that produce results longer than 1000 characters, you can use the notation {{ string_1 }}{{ string_2 }}.
Comparison Operations
8
Operator
Type
Designation
Application area
How to use
Example
1
Equality testing
Comparison Operations
==
Any context variables and values
{{ A == B }}
{{ A == B }}
Result: True if variable A is equal to variable B.
{{ A == 1 }}
Result: True if variable A is 1.
{{ A == 'one' }}
Result: True if variable A is equal to 'one'.
2
Inequality testing
Comparison Operations
!=
Any context variables and values
{{ A != B }}
{{ A != B }}
Result: True if variable A is not equal to variable B.
{{ A != 1 }}
Result: True if variable A is not equal to 1.
{{ A != 'один' }}
Result: True if variable A is not equal to 'one'.
3
Checking that the first operand is greater than the second
Comparison Operations
>
Numeric variables and numbers
{{ A > B }}
{{ A > B }}
Result: True if variable A is greater than variable B.
{{ A > 1 }}
Result: True if variable A is greater than 1.
4
Checking that the first operand is greater than or equal to the second
Comparison Operations
>=
Numeric variables and numbers
{{ A >= B }}
{{ A >= B }}
Result: True if variable A is greater than or equal to variable B.
{{ A >= 1 }}
Result: True if variable A is greater than or equal to 1.
5
Checking that the first operand is less than the second
Comparison Operations
<
Numeric variables and numbers
{{ A < B }}
{{ A < B }}
Result: True if variable A is less than variable B.
{{ A < 1 }}
Result: True if variable A is less than 1.
6
Checking that the first operand is less than or equal to the second
Comparison Operations
<=
Numeric variables and numbers
{{ A <= B }}
{{ A <= B }}
Result: True if variable A is less than or equal to variable B.
{{ A <= 1 }}
Result: True if variable A is less than or equal to 1.
No results from filter
Logic operations
8
Operator
Designation
Application area
How to use
Example
1
AND
and
Expressions and Boolean Variables
{{ A and B }}
{{ A and B }}
Result: True if both boolean variables A and B are true.
2
OR
or
Expressions and Boolean Variables
{{ A or B }}
{{ A or B }}
Result: True if one of the boolean variables A and B are true.
3
NOT
not
Expressions and Boolean Variables
{{ not A }}
{{ not A }}
Result: True if the Boolean variable A is false.
{{ not A == 1 }}
Result: False if variable A is 1.
4
Parentheses for grouping expressions
(expr)
Expressions and Boolean Variables
{{ (A) }}
{{ (A > 0) and (A < 10) }}
Result: True if variable A is greater than 0 and less than 10.
No results from filter
Other operations
8
Operator
Designation
Application area
How to use
Example
1
Finding the first operand in the second
in
Context Variables and Expressions
Line
{{ 's' in 'str' }}
List of numbers
{{ num in [num1,num2,...] }}
List of strings
{{ 'str' in ['str1','str2',...] }}
Dictionary
{{ key in { key1:val1, key2:val2, ... } }}
{{ 'A' in 'ABC' }}
Result: True.
{{ 1 in [1,2,3] }}
Result: True.
{{ 'ABC' in ['ABC','DEF','GHI'] }}
Result: True.
{{ 1 in {1: 'A', 2: 'B', 3: 'C'} }}
Result: True.
2
Testing an operand for a property
is
Tests
{{ A is B }}
Described in the table:
3
Applies a filter to an operand
|
Filters
{{ A|B }}
Described in the table:
4
Casting operands to strings and concatenating them
~
Context variables and strings
{{ "string" ~ var }}
{{ var ~ "string" }}
{{ "Hello " ~ name ~ "!" }}
Result: Hello John! (if the name variable contains the value John).
5
Getting an object's attribute
. / []
Any context variables
{{ A.B }}
{{ A['B'] }}
{{ ['A','B','C'].0 }}
Result: A.
{{ {'one': 'A', 'two': 'B', 'three': 'C'}['two'] }}
Result: B.
No results from filter
Tests
Test
Designation
How to use
Example
1
Checking that a variable exists in the context
defined
{{ A is defined }}
{{ A is defined }}, A exists
Result: True.
{{ A is defined }}, A doesn't exist
Result: False.
2
Checking that a variable does not exist in the context
undefined
{{ A is undefined }}
{{ A is undefined }}, A exists
Result: False.
{{ A is undefined }}, A doesn't exist
Result: True.
3
Checking against regular expression
match
{{ A is match ... }}
{{ A is match("[regular_expression]") }} Result:
True if A matches the regular expression;
False if A does NOT match the regular expression;
False if A is not in string format;
False if the regular expression is written incorrectly
Important: the check is case sensitive.
4
Checking that an object is a number
number
{{ A is number }}
{{ 5 is number }}
Result: True.
{{ 'five' is number }}
Result: False.
5
Checking that an object is an even number
even
{{ A is even }}
{{ 22 is even }}
Result: True.
{{ 11 is even }}
Result: False.
6
Checking that an object is an odd number
odd
{{ A is odd }}
{{ 46 is odd }}
Result: False.
{{ 15 is odd }}
Result: True.
7
Checking that an object is an integer
integer
{{ A is integer }}
{{ 1012 is integer }}
Result: True.
{{ 10.12 is integer }}
Result: False.
8
Checking that an object is a real number
float
{{ A is float }}
{{ 3.55 is float }}
Result: True.
{{ 35 is float }}
Result: False.
9
Checking that an object is divisible by a number
divisibleby
{{ A is divisibleby(B) }}
Important:
If A = True and B = 1, then True will be returned, since True is 1, and 1 is divisible by 1.
If A = False, then True will be returned, since False is 0, and 0 is divisible by any number (except 0).
{{ 4 is divisibleby(2) }}
Result: True.
{{ 6 is divisibleby(5) }}
Result: False.
{{ 5 is divisibleby(0) }}
Result: False.
{{ true is divisibleby(1) }}
Result: True.
{{ false is divisibleby(10) }}
Result: True.
10
Checking that an object is a string
string
{{ A is string }}
{{ 'one' is string }}
Result: True.
{{ 1 is string }}
Result: False.
11
Checking that a string is lowercase
lower
{{ A is lower }}
{{ 'hello' is lower }}
Result: True.
{{ 'Hello' is lower }}
Result: False.
{{ 'HELLO' is lower }}
Result: False.
12
Checking that a string is in upper case
upper
{{ A is upper }}
{{ 'BYE' is upper }}
Result: True.
{{ 'Bye' is upper }}
Result: False.
{{ 'bye' is upper }}
Result: False.
13
Checking that an object is a dictionary
mapping
{{ A is mapping }}
{{ {1:'A', 2:'B', 3:'C'} is mapping }}
Result: True.
{{ [1, 2, 3] is mapping }}
Result: False.
14
Checking that an object is a sequence (string, list, or dictionary)
sequence
{{ A is sequence }}
{{ '12345' is sequence }}
Result: True.
{{ [1, 2, 3] is sequence }}
Result: True.
{{ {1:'A', 2:'B', 3:'C'} is sequence }}
Result: True.
{{ 12345 is sequence }}
Result: False.
15
Checking that an object is a boolean
boolean
{{ A is boolean }}
{{ A is boolean }}, A = true
Result: True.
{{ A is boolean }}, A = 4
Result: False.
16
Checking that an object is true
true
{{ A is true }}
{{ A is true }}, A = true
Result: True.
{{ A is true }}, A = 1
Result: False.
17
Checking that an object is false
false
{{ A is false }}
{{ A is false }}, A = false
Result: True.
{{ A is false }}, A = 2
Result: False.
18
Checking that an object does not have a value (the value is None or Null (converted to None during ER parsing))
none
{{ A is none }}
{{ A is none }}, A = none
Result: True.
{{ A is none }}, A = ''
Result: False.
19
Equality testing (equivalent to the == operator)
eq
{{ A is eq B }}
{{ 123 is eq 123 }}
Result: True.
{{ 'abc' is eq 'ABC' }}
Result: False.
{{ 456 is eq '456' }}
Result: False.
20
Testing for inequality (equivalent to the != operator)
ne
{{ A is ne B }}
{{ 123 is ne 124 }}
Result: True.
{{ 'D' is ne 'D' }}
Result: False.
21
Checking that the first operand is greater than the second (equivalent to the > operator)
gt
{{ A is gt B }}
{{ 5 is gt 3 }}
Result: True.
{{ (-1.12) is gt 1.13 }}
Result: False.
{{ '5a' is gt '33' }}
Result: True.
{{ '222' is gt '33' }}
Result: False.
22
Checking that the first operand is greater than or equal to the second (equivalent to the >= operator)
ge
{{ A is ge B }}
{{ 12.12 is ge 12.1199 }}
Result: True.
{{ 15 is ge 15 }}
Result: True.
{{ 0 is ge (-15) }}
Result: False.
23
Checking that the first operand is less than the second (equivalent to the < operator)
lt
{{ A is lt B }}
{{ 0.001 is lt 1 }}
Result: True.
{{ (-1) is lt (-1.5) }}
Result: False.
24
Checking that the first operand is less than or equal to the second (equivalent to the <= operator)
le
{{ A is le B }}
{{ 21 is le 22 }}
Result: True.
{{ (-1.11) is le (-1.11) }}
Result: True.
{{ (1) is le (-12) }}
Result: False.
25
Finding the first operand in the second
in
{{ A in B }}
Described here:
@Finding the first operand in the second
There are no rows in this table
Syntax filters
Filter
Designation
How to use
Example
1
Adding a default value
default
{{ A|default(B) }}
The value B will only be used if variable A is not in the context.
{{ A|default(B,true) }}
The value B will be used in cases where variable A is not in the context or if variable A is cast to False (0, empty string, empty array, empty list).
{{ A|default('A is not defined') }}, A = 1
Result: 1.
{{ A|default('A is not defined') }}, A doesn't exist
Result: A is not defined.
{{ A|default('A is empty', true) }}, A = ''
Result: A is empty.
{{ A|default('A is not defined', true) }},
A doesn't exist
Result: A is not defined.
2
Convert to integer
int
{{ A|int }}
{{ 2.53|int }}
Result: 2.
{{ '-15'|int }}
Result: -15.
{{ '12abc'|int }}
Result: 0.
3
Convert to real number
float
{{ A|float }}
{{ 2|float }}
Result: 2.0.
{{ '-122'|float }}
Result: -122.0.
{{ '1a'|float }}
Result: 0.0.
4
Returning the absolute value of a number
abs
{{ A|abs }}
Limitations: Works only with integers and real numbers.
{{ -1|abs }}
Result: 1.
{{ -0.1111|abs }}
Result: 0.1111.
5
Rounding a number
round
{{ A|round }}
Rounding to the nearest whole part.
{{ A|round(B, C) }}
B — number of decimal places.
C — rounding type:
common — rounding up or down.
ceil — rounding up.
floor — rounding down.
{{ 42.12345|round }}
Result: 42.0.
{{ -1.538675|round(3,'common') }}
Result: -1.539.
{{ 0.5712|round(2, 'ceil') }}
Result: 0.58.
{{ 121.67354|round(3, 'floor') }}
Result: 121.673.
{{ 0.11111|round|int }}
Result: 0.
6
Convert to string
string
{{ A|string }}
{{ 12345|string }}
Result: 12345.
{{ [1,2,3,4,5]|string }}
Result: [1, 2, 3, 4, 5] (the whole list is a string).
{{ {1: 'A', 2: 'B', 3: 'C'}|string }}
Result: {1: 'A', 2: 'B', 3: 'C'} (the whole dictionary is a string).
7
Convert to list
list
{{ A|list }}
{{ 'abcdefg'|list }}
Result: ['a', 'b', 'c', 'd', 'e', 'f', 'g'].
{{ {'one': 'A', 'two': 'B', 'three': 'C'}|list }}
Result: ['one', 'two', 'three'].
{{ 12345|list }}
Result: False.
8
Convert to lowercase
lower
{{ A|lower }}
{{ 'AbcDefg'|lower }}
Result: abcdefg.
{{ ['A','B','C']|lower }}
Result: ['a', 'b', 'c'].
{{ {'one': 'A', 'two': 'B', 'three': 'C'}|lower }}
Result: {'one': 'a', 'two': 'b', 'three': 'c'}.
9
Convert to uppercase
upper
{{ A|upper }}
{{ 'AbcDefg'|upper }}
Result: ABCDEFG.
{{ ['a','b','c']|upper }}
Result: ['A', 'B', 'C'].
{{ {'one': 'a', 'two': 'b', 'three': 'c'}|upper }}
Result: {'ONE': 'A', 'TWO': 'B', 'THREE': 'C'}.
10
Converting the first letter in a string to uppercase
capitalize
{{ A|capitalize }}
Limitations: Works only with strings.
{{ 'abc'|capitalize }}
Result: Abc.
11
Convert to capital case (i.e. words will start with uppercase letters, all other characters will be lowercase)
title
{{ A|title }}
Important: list and dictionary elements are converted to lowercase.
{{ 'hello'|title }}
Result: Hello.
{{ 'hELLO'|title }}
Result: Hello.
{{ {'one': 'A', 'two': 'B', 'three': 'C'}|title }}
Result: {'one': 'a', 'two': 'b', 'three': 'c'}.
12
First element of a sequence (string, list, or dictionary)
first
{{ A|first }}
{{ '12345'|first }}
Result: 1.
{{ [1, 2, 3]|first }}
Result: 1.
{{ {1:'A', 2:'B', 3:'C'}|first }}
Result: 1.
{{ 12345|first }}
Result: False.
13
Last element of a sequence (string, list, or dictionary)
last
{{ A|last }}
{{ '12345'|last }}
Result: 5.
{{ [1, 2, 3]|last }}
Result: 3.
{{ {1:'A', 2:'B', 3:'C'}|last }}
Result: 3.
{{ 12345|last }}
Result: False.
14
Number of elements in a sequence (string, list, or dictionary)
length
{{ A|length }}
{{ 'abcdefg'|length }}
Result: 7.
{{ [1, 2, 3]|length }}
Result: 3.
{{ {1:'A'}|length }}
Result: 1.
{{ 111|length }}
Result: False.
15
Sum of sequence elements
sum
{{ A|sum }}
Important: if the sequence is empty, then 0 is returned.
{{ [1,2,3,4,5]|sum }}
Result: 15.
{{ {1: 'A', 2: 'B', 3: 'C'}|sum }}
Result: 6.
{{ ''|sum }}
Result: 0.
16
Getting the smallest element value of a sequence
min
{{ A|min }}
{{ [1, 0.001, -1]|min }}
Result: -1.
{{ ['1', '0.001', '-1']|min }}
Result: -1.
{{ ['b', 'f', 'a']|min }}
Result: a.
{{ {'1': 'A', '2': 'B', '3': 'C'}|min }}
Result: 1.
17
Getting the largest element value of a sequence
max
{{ A|max }}
{{ [1, 0.001, -1]|max }}
Result: 1.
{{ ['1', '0.001', '-1']|max }}
Result: 1.
{{ ['b', 'f', 'a']|max }}
Result: f.
{{ {'1': 'A', '2': 'B', '3': 'C'}|max }}
Result: 3.
18
Getting a random element
random
{{ A|random }}
Limitations: Works only with lists and arrays.
{{ [-22, 4.110263, 353, 124]|random }}
Result: 4.110263.
{{ ['-22', '4.110263', '353', '124']|random }}
Result: -22.
{{ ['apple', 'orange', 'banana']|random }}
Result: banana.
19
Replacing a sequence element
replace
{{ A|replace }}
{{ "Hello World"|replace("Hello", "Goodbye") }}
Result: Goodbye World.
{{ ['A','B','C']|replace('A', 'D') }}
Result: ['D', 'B', 'C'].
20
Reversing a sequence
reverse
{{ A|reverse }}
Important: To reverse a list or dictionary, you must add |list to convert the result to a list, otherwise the variable will be empty.
{{ 'abcdefg'|reverse }}
Result: gfedcba.
{{ '12345'|reverse }}
Result: 54321.
{{ ['a', 'b', 'c']|reverse|list }}
Result: ['c', 'b', 'a'].
{{ {'1': 'A', '2': 'B', '3': 'C'}|reverse|list }}
Result: ['3', '2', '1'].
21
Adding a sequence element separator (string, list, or dictionary)
join
{{ A|join }}
{{ A|join(B) }}
{{ '123'|join(',') }}
Result: (1, 2, 3).
{{ [1, 2, 3]|join }}
Result: 123.
{{ {'one': 'A', 'two': 'B', 'three': 'C'}|join('!') }}
Result: one!two!three.
{{ {1:'A', 2:'B', 3:'C'}.values()|join(", ") }}
Result: A, B, C.
{{ {1:'A', 2:'B', 3:'C'}.keys()|join("!")|list }}
Result: ['1', '!', '2', '!', '3'].
22
Converting a sequence to a unique list
unique
{{ A|unique|list }}
Limitations: only works with adding |list.
{{ 'abcab'|unique|list }}
Result: gfedcba.
{{ '12345'|reverse }}
Result: 54321.
{{ ['a', 'b', 'c']|reverse|list }}
Result: ['c', 'b', 'a'].
{{ {'1': 'A', '2': 'B', '3': 'C'}|reverse|list }}
Result: ['3', '2', '1'].
23
Sort a sequence
sort
{{ A|sort }}
{{ A|sort(r, c, a) }}
r(reverse) - sort in descending order, not ascending order.
c(case_sensitive) - sort upper and lower case separately.
a(attribute) - value or key to sort.
{{ [1,4,3,2,5]|sort }}
Result: [1, 2, 3, 4, 5].
{{ ['b','a','c']|sort(reverse=true) }}
Result: ['c', 'b', 'a'].
{{ [{'id': 3, 'name': 'O'}, {'id': 1, 'name': 'E'}, {'id': 2, 'name': 'S'}]|sort(attribute='name') }}
Result: [{'id': 1, 'name': 'E'}, {'id': 3, 'name': 'O'}, {'id': 2, 'name': 'S'}].
{{ {4: 'c', 2: 'a', 3: 'b', 1:'d'}.values()|sort }}
Result: ['a', 'b', 'c', 'd'].
{{ {4: 'c', 2: 'a', 3: 'b', 1:'d'}.keys()|sort }}
Result: [1, 2, 3, 4].
24
Sort a dictionary by key or value
dictsort
{{ A|dictsort }}
{{ A|dictsort(by='key' }}
{{ A|dictsort(by='value' }}
Important: The dictionary is converted to a string by default.
{% for key, value in {"c": 1, "a": 4, "b": 2, "d": 3}|dictsort %} {{ key }}: {{ value }} {% endfor %}
Result: aaa: 4 b: 2 c: 1 d: 3.
{% for key, value in {"c": 1, "a": 4, "b": 2, "d": 3}|dictsort(by="key") %} {{ key }}: {{ value }} {% endfor %}
Result: a: 4 b: 2 c: 1 d: 3.
{{ {4: 'c', 2: 'a', 3: 'b', 1:'d'}|dictsort(by='value')|list }}
Result: [(2, 'a'), (3, 'b'), (4, 'c'), (1, 'd')].
25
Filter by attribute
map
{{ A|map(attribute='B')|map(attribute='C'|...|list }}
Important: if the object is an array, then the result must be converted to a list.
{{ [{'name': 'Anna', 'number': '123'}, {'name': 'Olga', 'number': '456'}, {'name': 'Evgeniya', 'number': '789'}]| map(attribute='name')|list }}
Result: ['Anna', 'Olga', 'Evgeniya'].
26
Grouping by number of elements in a loop
batch
{{ A|batch(l) }}
{{ A|batch(l, f) }}
l(linecount) - the number of groups into which the object will be divided.
f(fill_with) - the value that is used to fill missing elements.
{% for row in ['A','B','C']|batch(5, 'D') %}{{ row }}{% endfor %}
Result: ['A', 'B', 'C', 'D', 'D'].
{% for row in [1, 2, 3, 4]|batch(2) %}
{% for i in row %}{{ i }}{% endfor %}
{% endfor %}
Result:
12
34.
27
Convert to a list of lists
slice
{{ A|slice(s) }}
{{ A|slice(s, f) }}
s(slices) - the number of lists into which the object will be split.
f(fill_with) - the value that is used to fill missing elements.
{{ [1, 2, 3, 4, 5]|slice(2)|list }}
Result: [[1, 2, 3], [4, 5]].
{{ 'abc'|slice(5, 'd')|list }}
Result: [['a'], ['b'], ['c'], ['d'], ['d']].
28
Group by attribute
groupby
{{ A|groupby(a) }}
{{ A|groupby(a, d) }}
a(attribute) - attribute.
d(default) - default value used if an object in the list does not have this attribute.
items = [ {"name": "james", "type": "green"}, {"name": "john", "type": "blue"}, {"name": "jim", "type": "blue"}, {"name": "jessie", "type": "green"} ]
{% for type, items in items|groupby('type') %}
{{ type }}:
{% for item in items %}{{ item.name }} {% endfor %}
{% endfor %}
Result: blue: john jim green: james jessie.
29
Removing sequence elements that do not match the test
select
{{ A|select(B,C)|list }}
B - test from the table
C is the argument that is passed to test B.
{{ [1, 2, 3, 4, 5]|select('odd')|list }}
Result: [1, 3, 5] (нечетные числа).
{{ [1, 2, 3, 4, 5]|select('gt', 3)|list }}
Result: [4, 5] (числа, которые больше 3).
30
Removing sequence elements that do not match an attribute test
selectattr
{{ A|selectattr(B,C,D)|list }}
B is the attribute that needs to be extracted from the sequence.
C - test from the table
D is the argument that is passed to test C.
users = [ {"name": "james", "email": "james@james.com", "password": "123"}, {"name": "john", "email": "john@john.com"}, {"name": "jim", "email": "jim@jim.com"}, {"name": "jessie", "email": "jessie@jessie.com", "password": "123"} ] .
{{ users|selectattr('password','undefined')| map(attribute='email')|list }}{{ email }}
Result: ['john@john.com', 'jim@jim.com'].
table = [['685697115', 'evpolyntseva', 'Новосибирск'], ['658795243', 'Iourigaha', 'Москва'], ['683128920', 'jarondara', 'Казань'], ['630685929', 'blarar', 'Москва']] .
{{ table_all|selectattr(2, 'eq', "Москва")|map(attribute=1)|list }}
Result: ['Iourigaha', 'blarar'].
31
Removing sequence elements that match a test
reject
{{ A|reject(B,C)|list }}
B - test from the table
C is the argument that is passed to test B.
{{ [1, 2, 3, 4, 5]|reject('odd')|list }}
Result: [2, 4] (не нечетные числа).
{{ [1, 2, 3, 4, 5]|reject('gt', 3)|list }}
Result: [1, 2, 3] (числа, которые не больше 3).
32
Removing sequence elements that match a test by attribute
rejectattr
{{ A|rejectattr(B)|list }}
B is the attribute that needs to be extracted from the sequence.
C - test from the table
D is the argument that is passed to test C.
table = [['685697115', 'evpolyntseva', 'Новосибирск'], ['658795243', 'Iourigaha', 'Москва'], ['683128920', 'jarondara', 'Казань'], ['630685929', 'blarar', 'Москва']] .
{{ table|selectattr(2, 'eq', "Москва")|map(attribute=1)|list }}
Result: ['evpolyntseva', 'jarondara'].
33
Removing characters at the beginning and end of a line
trim
{{ A|trim }}
{{ A|trim(c) }}
c(chars) - the character or string that will be removed. Default is space.
{{ ' a '|trim }}
Result: a.
{{ 'bbbсbb'|trim('b') }}
Result: c.
34
String truncation
truncate
{{ A|truncate(len) }}
{{ A|truncate(len,k,e,l) }}
len(length) - length of the truncated string. Default is 255.
k(killwords) - True or False. True is specified if you need to trim the string by length, False if you need to discard the last trimmed word.
e(end) - sign at the truncation point. Default is ellipsis.
l(leeway) - limit of excess length.
{{ 'foo bar baz qux'|truncate(9) }}
Result: foo... .
{{ 'foo bar baz qux'|truncate(9, True) }}
Result: foo ba... .
{{ 'foo bar baz qux'|truncate(11, False, '', 0) }}
Result: foo bar.
35
Word count
wordcount
{{ A|wordcount }}
{{ 'Hello World!'|wordcount }}
Result: 2.
{{ ['a a a','b','c']|wordcount }}
Result: 5.
{{ {'one': 'a', 'two': 'b b b', 'three': 'c'}|wordcount }}
Result: 8.
36
Format a number to file size
filesizeformat
{{ A|filesizeformat }}
{{ 100000 | filesizeformat }}
Result: 100.0 kB.
{{ 25000000 | filesizeformat }}
Result: 25.0 MB.
37
URL encoding