Extended expressions¶
Extended expression syntax is available when Python Liquid is configured with an
liquid.ExtendedParser. See Tokenizers and parsers for more information.
Unlike the legacy parser, the extended parser makes expressions composable. Logical operators, arithmetic operators, conditional expressions and filters can be combined freely wherever an expression is expected. This applies not just to output statements, but also to tag arguments and filter arguments.
Logical expressions¶
The extended parser adds a logical not operator and parentheses, ( and ), for
grouping terms.
In standard Liquid you might write something like this.
{% if user.active or user.trial %}
{% unless user.suspended %}
Access granted.
{% endunless %}
{% endif %}
Using not and parens, we could write it like this.
{% if (user.active or user.trial) and not user.suspended %}
Access granted.
{% endif %}
Or, with a ternary expression, like this.
{{ "Access granted." if (user.active or user.trial) and not user.suspended }}
Short circuit, last value¶
and and or are short circuiting operators with last value semantics, just like
Python.
Instead of a multi-branch {% if %} tag:
{% if user.nickname %}
{{ user.nickname }}
{% elsif user.name %}
{{ user.name }}
{% else %}
Guest
{% endif %}
We can use a single or expression directly inside an output statement.
{{ user.nickname or user.name or "Guest" }}
Or, if user is potentially undefined or null:
{{ user.nickname if user and user.nickname else "Guest" }}
Often, we can use or instead of the default filter.
{{ product.sale_price | default: product.price }}
{{ product.sale_price or product.price }}
Logical operators are not limited to left values either, they can be used in filter arguments.
{{ items | join: (separator or ", ") }}
Arithmetic¶
The extended parser adds arithmetic operators +, -, *, / and %, which
behave exactly the same as their equivalent filters, type coercion and all.
Instead of using filters like this.
{% assign total = price | times: quantity | plus: shipping %}
We can use arithmetic operators and parens, if needed.
{% assign total = price * quantity + shipping %}
Like all operators, arithmetic is not limited to {% assign %} and output statements.
{% if price * quantity > budget %}
Over budget!
{% endif %}
Ternary Expressions¶
The extended parser supports Python-style ternary expressions.
<expression> if <condition>[ else <expression>]
If the condition is falsy and no else branch is given, the expression evaluates to
the special value NOTHING, which is similar to null.
{{ "Welcome back!" if user else "Please sign in." }}
Keyword assignment¶
The extended parser accepts = as well as : between named arguments and their
value expressions. In this example we use the with tag to create temporary,
block-scoped variables.
{% with
a = 1,
b = 3.4,
c = a + b,
%}
{{ a }} + {{ b }} = {{ c }}
{% endwith %}
Operator Precedence¶
Priority |
Operator Category |
Operators / Syntax |
Examples |
|---|---|---|---|
1 (Highest) |
Primary & Grouping |
Literals, Variables, |
|
2 |
Unary |
|
|
3 |
Multiplicative |
|
|
4 |
Additive |
|
|
5 |
Filter Pipe |
| |
|
6 |
Comparison & Ordering |
|
|
7 |
Logical NOT |
|
|
8 |
Logical AND |
|
|
9 |
Logical OR |
|
|
10 (Lowest) |
Inline Conditional |
|
|