Expressions
4 minute read
Expressions get replaced with values when a template is rendered.
Expressions appear inside {{ ... }} tags.
Literals
The simplest form of expressions.
Boolean
true is always true and false is always false.
true
false
Dict
A dict in Python is a structure that combines keys and values.
Keys must be unique and always have exactly one value.
{
"foo": "Hello World"
}
Integers
Integers are whole numbers without a decimal part.
The underscore (_) character can be used to separate groups for legibility.
42
123_456
Floating point numbers
Floating Point Numbers can be written using a period (.) as a decimal mark.
They can be written in scientific notation with an upper or lowercase e to indicate the exponent part.
The underscore (_) character can be used to separate groups for legibility, but cannot be used in the exponent part.
42.23
42.1e2
123_456.789
List
Everything between two square brackets is a list ([...]).
Lists are useful for storing sequential data to be iterated over.
["List", "of", "Objects"]
String
Everything between two double or single quotes is a string.
They are useful whenever you need a string in the template.
"Hello World"
Tuples
Tuples are similar to lists that cannot be modified (“immutable”) and usually used to represent items of two or more elements.
If a tuple only has one item, it must be followed by a comma.
("Tuple",)
("Tuple", "of", "values")
Math
Add
Adds two objects together.
Usually the objects are numbers. See the ~ operator for string concatenation.
{{ 1 + 1 }}
=> 2
Subtract
Subtracts the second number from the first number.
{{ 3 - 2 }}
=> 1
Divide
Divide two numbers
The return value will be a Floating Point Number.
{{ 1 / 2 }}
=> 0.5
Divide two numbers and return the truncated integer result
{{ 20 // 7 }}
=> 2
Calculate the remainder of an integer division
{{ 11 % 7 }}
=> 4
Multiply
Multiply the left operand with the right one.
This can also be used to repeat a string multiple times.
{{ 2 * 2 }}
=> 4
{{ "!" * 8 }}
=> !!!!!!!!
Raise to the power
Raise the left operand to the power of the right operand.
{{ 2 ** 3 }}
=> 8
Comparisons
Equality (==)
Compares two objects for equality.
{{ "hello" == "hello" }}
=> true
Inequality (!=)
Compares two objects for inequality.
{{ "hello" != "hello" }}
=> false
Greater than (>)
Left hand side is greater than the right hand side.
{{ 5 > 4 }}
=> true
Greater than or equal to (>=)
Left hand side is greater or equal to the right hand side.
{{ 5 >= 5 }}
=> true
Less than (<)
Left hand side is lower than the right hand side.
{{ 5 < 4 }}
=> false
Less than or equal to (<=)
Left hand side is lower or equal to the right hand side.
{{ 5 <= 5 }}
=> true
Logic
and
Returns true if the left and the right operand are true.
{% set foo = 2 %}
{{ foo > 1 and foo < 5 }}
=> true
or
Returns true if the left or the right operand are true.
{% set foo = 3 %}
{{ foo < 5 or foo == 3 }}
=> true
not
Negates a statement using the is and in operators. All other expressions will require a prefix notation.
{{ foo is not bar }}
{{ foo not in bar }}
{{ not (foo and bar) }}
(expr)
Group expressions in parentheses.
{% set foo = 2 %}
{{ (foo > 1) and (foo < 5) }}
=> true
Other operators
in
Perform a sequence or mapping containment test. Returns true if the left operand is contained in the right.
{{ 1 in [1, 2, 3] }}
=> true
is
Performs a Test.
{{ foo is bar }}
|
Applies a Filter.
{{ foo | lower }}
~
Converts all operands into strings and concatenates them.
{% set foo = "world" %}
{{ "Hello " ~ foo ~ "!" }}
=> Hello world!
. or []
Get an attribute of an object.
{{ foo.bar }}
{{ foo["bar"] }}
If expression
Learn about using inline if expressions.
This is useful in some situations. The general syntax is:
{# <do something> if <something is true> else <do something else> #}
{{ "[{}]".format(page.title) if page.title }}
The else part is optional. If not provided, the else block implicitly evaluates into an Undefined object.
Also refer to Control Structures: if.