Twig Operators
Operators let you perform operations like comparison , containment , logic , math , or tests .
The operator precedence is as follows, with the lowest-precedence operators listed first: b-and, b-xor, b-or, or, and, ==, !=, <, >, >=, <=, in, matches, starts with, ends with, .., +, -, ~, *, /, //, %, is, **, |, and [ ]
Miscellaneous operators
There are a few operators that don't fit into a category.
Operator | Description |
---|---|
| | |
.. | |
~ | |
?: | |
?? |
Comparison
The following comparison operators are supported in any expression: ==, !=, <, >, >=, and <=.
You can also check if a string starts with or ends with another string or use a regular expression.
If you are comparing one variable to another you would use the following syntax:
{% if variableA == variableB %}
{% endif %}
Note that both variables are referenced by their name only, quotes are not used, and {{ }} is not used.
Operator | Description |
---|---|
== | |
!= | |
< | |
<= | |
> | |
>= | |
starts with | |
ends with | |
matches |
Containment
The in operator lets you see if the left value is contained in the right value.
Operator | Description |
---|---|
in |
Logic
You can combine multiple expressions with the following operators. These are typically done with the if tag.
Operators are case sensitive.
Twig also support bitwise operators (b-and, b-xor, and b-or).
Operator | Description |
---|---|
and | |
&& | |
or | |
|| | |
not |
Math
Twig allows you to calculate with values. The following operators are supported.
Operator | Description |
---|---|
+ | |
- | |
/ | |
% | |
// | |
* | |
** |
Tests
The is operator performs tests. Tests can be used to test a variable against a common expression. The right operand is name of the test. See the Tests page for a full list of available tests.
Operator | Description |
---|---|
is |