Operators
Comparisons
The following comparison operators are supported in any expression: ==, !=, <, >, >=, and <=.
You can also check if a string starts with or ends with another string:
{% if 'Fabien' starts with 'F' %}
{% endif %}
{% if 'Fabien' ends with 'n' %}
{% endif %}
Logic
You can combine multiple expressions with the following operators:
- and: Returns true if the left and the right operands are both true.
- or: Returns true if the left or the right operand is true.
- not: Negates a statement.
- (expr): Groups an expression.
Math
Twig allows you to calculate with values. This is rarely useful in templates but exists for completeness' sake. The following operators are supported:
- +: Adds two objects together (the operands are casted to numbers). {{ 1 + 1 }} is 2.
- -: Subtracts the second number from the first one. {{ 3 - 2 }} is 1.
- /: Divides two numbers. The returned value will be a floating point number. {{ 1 / 2 }} is {{ 0.5 }}.
- %: Calculates the remainder of an integer division. {{ 11 % 7 }} is 4.
- //: Divides two numbers and returns the floored integer result. {{ 20 // 7 }} is 2, {{ -20 // 7 }} is -3 (this is just syntactic sugar for the round filter).
- : Multiplies the left operand with the right one. {{ 2 2 }} would return 4.
- : Raises the left operand to the power of the right operand. {{ 2 3 }} would return 8.
Is
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:
{# find out if a variable is odd #}
{{ name is odd }}
Tests can accept arguments too:
{% if post.status is constant('Post::PUBLISHED') %}
Tests can be negated by using the is not operator:
{% if post.status is not constant('Post::PUBLISHED') %}
{# is equivalent to #}
{% if not (post.status is constant('Post::PUBLISHED')) %}
Go to the tests page to learn more about the built-in tests.
In
The in operator performs containment test.
It returns true if the left operand is contained in the right:
{# returns true #}
{{ 1 in [1, 2, 3] }}
{{ 'cd' in 'abcde' }}
To perform a negative test, use the not in operator:
{% if 1 not in [1, 2, 3] %}
{# is equivalent to #}
{% if not (1 in [1, 2, 3]) %}
Other
- The following operators don't fit into any of the other categories:
- |: Applies a filter.
- ..: Creates a sequence based on the operand before and after the operator
{{ 1..5 }}
{# equivalent to #}
{{ range(1, 5) }}
- Note that you must use parentheses when combining it with the filter operator due to the operator precedence rules:
(1..5)|join(', ')
- ~: Converts all operands into strings and concatenates them. {{ "Hello " ~ name ~ "!" }} would return (assuming name is 'John')Hello John!.
- ., []: Gets an attribute of an object.
- ?:: The ternary operator:
{{ foo ? 'yes' : 'no' }}
{# as of Twig 1.12.0 #}
{{ foo ?: 'no' }} is the same as {{ foo ? foo : 'no' }}
{{ foo ? 'yes' }} is the same as {{ foo ? 'yes' : '' }}
- ??: The null-coalescing operator:
{# returns the value of foo if it is defined and not null, 'no' otherwise #}
{{ foo ?? 'no' }}
Updated over 5 years ago