Advanced
The following documentation goes into more details on how to use the personalization and templating engine.
You will learn about builtin filters, how to control whitespace if you include if
/else
statements, what kind of comparison you can use and more.
Filters
Sidenote - filters with arguments
You may have noticed some filters take arguments. When this is the case, there is two way to give the arguments:
- Using a named argument as such:
join(separator=',')
- Directly giving the value as such:
join(',')
In the following reference we will denote arguments that can be given unnamed as such: argName?: type
where the ?
indicates the argument is optional.
General filters
Filters for numbers only
Filters for strings only
Filters for tag collections only
Chaining filters
It is possible to chain filters to produce more complex results, however you need to make sure the input and output types are compatible between filters.
For example, you can't call formatDate
on a number or even a string, the type has to be a date. Look at the table above to know what filters are compatible.
Here is a valid example of chaining multiple filters:
Your next exam is on {{ t.exams|last|date|formatDate('yyyy-MM-dd') }}
Given a user with this tag collection t.exams
: ["2017-10-09T14:53:54Z", "2017-10-11T16:53:54Z"]
the example evalutes to:
Your next exam is on 2017-10-11
As you can see we take the last
value of the tag collection which returns a string
, then pass that to date
which parses it and returns a date
type. Finally we pass that to formatDate
.
Expression
An expression is something that returns any kind of value. It can a math operation, a comparison, a function call, a reference to an attribute or tag or even a literal value.
Examples:
{% set $firstName = c.first_name|upper %}
{% set $var = 3 + 10 * 20 %}
{% set $minor = c.age < 18 %}
{% set $lastTag = c.interests|last|upper %}
{% set $tomorrow = now + 24h %}
{% set $foo = 'foobar' %}
Expression are used in:
if
/else if
conditions- variable assignment
- expression output
{{ ... }}
Comparison
Type comparison rules
When comparing two values, they have to be of the same type otherwise it won't work.
The rules are as follows:
- A
string
can only be compared with anotherstring
- A
date
can only be compared with anotherdate
- A
duration
can be compared with anotherduration
or anumber
. When comparing with anumber
it is treated as days; in other words{{ $myDuration == 3 }}
is equivalent to{{ $myDuration == 3d }}
. - A
distance
can be compared with anotherdistance
or anumber
. When comparing with anumber
it is treated as meters; in other words{{ $myDistance == 200 }}
is equivalent to{{ $myDistance == 200m }}
- A
number
can be compared with anothernumber
or aboolean
.
Operators
==
compares two values for equality!=
compares two values for inequality>
returns true if the left hand side is greater than the right hand side>=
returns true if the left hand side is greater than or equal to the right hand side<
returns true if the left hand side is lower than the right hand side<=
returns true if the left hand side is lower than or equal to the right hand side
Combining comparisons
As in any programming languages, you can combine comparisons easily:
and
returns true if both the left hand side and the right and side are trueor
returns true if either the left hand side is true or the right hand side is truenot
negates a statement(
and)
to group expressions.
Example:
{% set $isYoungAdult = c.age > 18 and c.age < 26 %}
{% set $hasEnoughBandwidth = c.has_fiber or (c.has_mobile and c.mobile_connection_type == '4g' %}
{% if $isYoungAdult and $hasEnoughBandwidth %}
Don't forget you can stream the match in 4k by subscribing !
{% else %}
Don't forget you can stream the match by subscribing !
{% endif %}
Data types
Standard
Standard types include:
- string. You can write a literal string by using a
'
character like this:'This is a string'
. To use a'
inside your string you need to double it like this:'It''s great'
- integer. You can write a literal integer like this:
230
. - float. You can write a literal float like this:
20.30
- boolean. A boolean is either
true
orfalse
Date
Date is a special type that can't be created by a literal. However they are produced in a couple of cases:
- if an attribute is a date (that is either a
NSDate
or ajava.util.Date
for iOS and Android respectively). - by converting a UNIX timestamp (number of seconds since January 1, 1970):
{{ 1491814800|date|formatDate('yyyy-MM') }}
- by using the keyword
now
which returns the date at the time of execution
Duration
Duration is a special integer with a time unit. Units can be:
- days:
40d
- hours:
24h
- minutes:
30m
- seconds:
46s
Distance
Distance is a special integer with a distance unit. It is also always positive. Units can be:
- meters:
5600m
- kilometers:
83km
Casting rules
You can cast values into different types by using a casting operation, provided the types are compatible. The casting operation looks like this:
{{ c.my_string_attribute|float|int }}
Casting looks exactly like a filter but it simply converts the original value to the type if the rules allow it.
The rules of casting are as follows:
from / to | string | int | float | bool | date | distance | duration |
string | ✓ | ✓ | ✓ | ✓1 | ✓ | ✓ | |
int | ✓ |