Expressions and operators
An expression is what goes inside {{ }}, after if or elsif, after assign name =, and in a few other tag positions.
#Filters
A filter transforms the value on its left. Chain filters with |; they apply left to right. Arguments follow a colon, separated by commas.
{{ " hello world " | strip | capitalize | append: "!" }}
{{ "a,b,c" | split: "," | join: " + " }}
{{ settings.text | truncate: 8, "…" }}{"settings": {"text": "Vascula templates"}}Hello world!
a + b + c
Vascula…Some filters also take named arguments, written name: value after any positional ones:
{{ settings.on | default: true, allow_false: true }}{"settings": {"on": false}}falseA filter argument is a single value: a literal, a variable path or a range. To pass the result of another filter, assign it first.
{% assign suffix = settings.unit | upcase %}{{ settings.amount | append: suffix }}{"settings": {"amount": 3, "unit": "kg"}}3KGEvery built-in filter is described in Filters. Applications add their own, such as formatting money or building image URLs.
#Comparisons
==, !=, <, >, <=, >= and contains compare two values. The rules for each are in Values: comparing values. Filters bind tighter than comparisons, so each side is filtered first:
{% if settings.name | downcase == "ada" %}hello Ada{% endif %}{"settings": {"name": "ADA"}}hello AdaA comparison takes exactly two sides: a == b == c is a compilation error.
#and, or
and and or combine conditions. They return one of their operands, not a new true or false:
a or bisawhenais truthy, otherwiseb.a and bisbwhenais truthy, otherwisea.
That makes or a convenient fallback:
{{ settings.nickname or settings.name }} / {{ settings.missing or "Guest" }} / {{ settings.name and "has a name" }}{"settings": {"name": "Ada"}}Ada / Guest / has a nameThe right side is only evaluated when needed, so customer and customer.name never reads a field of nothing.
#Precedence
From tightest to loosest:
- A value with its filters:
x | upcase - A comparison:
a == b andor
There are no parentheses for grouping, so a or b and c means a or (b and c). For anything more involved, assign intermediate results to variables. Parentheses are only used for ranges.
{% if false or true and false %}yes{% else %}no{% endif %}no#There is no not
Use unless, !=, or == false:
{% unless settings.hidden %}shown{% endunless %} {% if settings.count != 0 %}nonzero{% endif %}{"settings": {"hidden": false, "count": 2}}shown nonzero#Ranges
(from..to) is the list of integers from from to to, inclusive. Each end is a number or a variable. A range whose end is below its start is empty.
{{ (1..4) | join: "," }} | {% for i in (1..settings.n) %}{{ i }}{% endfor %} | [{{ (3..1) | join: "," }}]{"settings": {"n": 3}}1,2,3,4 | 123 | []Next: Conditionals.