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. 

OperatorDescription
|

The pipe charactor | applies a filter.

{{ profile.firstName|capitalize }}

{% if item.itemName|lower == 'my name' %}
{% endif %}

{{ post.content|truncate_html(100) }}

{{ name|lower|truncate(20) }}

Note that best practices call for no spaces before and after the pipe character.

..

The .. operator creates a sequence based on the value before and after the operator. It's the same as the range function.

{{ 1..5 }} is the same as {{ range(1, 5) }}

If you are combing this with the filter operator then you need to use parentheses due to the operator precedence rules mentioned above.

{{ (1..5)|join(', ') }}

 

Iterate over a sequence of numbers with the for tag.

{% for i in 0..10 %}
    {{ i }}<br>
{% endfor %}

The above will output all numbers from 0 to 10 with each on a new line.

You can do a similar thing with letters.

{% for letter in 'a'..'z' %}
    {{ letter }}<br>
{% endfor %}

The .. operator can take expressions (filters) at both sides

{% for letters in 'a'|upper..'z'|upper %}
    {{ letter }}<br>
{% endfor %}

~

The ~ operator converts the value to the left and right of it to strings and concatenates them.

Assuming that profile.firstName is "John" the following would output "Hello John!".

{{ "Hello " ~ profile.firstName ~ "!" }}

You can aso use ~ when setting values.

{% set field.class = field.class ~ ' newClass' %}

?:

?: is the ternary operator. It can be used in it's long-form or short-form.

A ternary operator is basically a condensed version of an if/else statement.

Long form

If the variable exists, output it. Otherwise, output something else.

{{ variable ? variable : 'Variable does not exist' }}

If the test is true output something, otherwise output something else.

{{ profile.firstName == 'Bob' ? 'You are Bob' : 'You are not Bob' }}

Short form - option 1

{{ variable ?: 'Variable does not exist' }}

The above code is the same as 

{{ variable ? variable : 'Variable does not exist' }}

Short form - option 2

{{ variable ? "it exists!" }}

The above code is the same as

{{ variable ? "it exists!" : "" }}

??

?? is the null-coalescing operator.

If the left value is defined and not null then it will be returned, otherwise returne the value on the right.

{{ variable ?? "value does not exist or is null" }}

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.

OperatorDescription
==

Compares the left value equals the right value.

{% if value1 == value2 %}
{% endif %}

Note: = is used for setting values. You must use two equal signs, ==, to check for equality between values.

!=

Compares the left value to the right value to see if they are not equal.

{% if 3 != 5 %}
{% endif %}

<

Compares the left value with the right value to see if the left value is less than the right value.

{% if 3 < 4 %}
{% endif %}

<=

Compares the left value with the right value to see if the left value is less than or equal to the right value.

{% if 3 <= 4 %}
{% endif %}

>

Compares the left value with the right value to see if the left value is greater than the right value.

{% if 4 > 2 %}
{% endif %}

>=

Compares the left value with the right value to see if the left value is greater than or equal to the right value.

{% if 4 >= 2 %}
{% endif %}

starts with

Checks to see if a string starts with a certain value.

{% if 'BranchCMS' starts with 'Bran' %}
{% endif %}

ends with

Checks to see if a string ends with a certain value.

{% if 'BranchCMS' ends with 'CMS' %}
{% endif %}

matches

Use a regular expression to check a value.

{% if phone matches '/^[\\d\\.]+$/' %}
{% endif %}

Containment

The in operator lets you see if the left value is contained in the right value.

OperatorDescription
in

The in operator performs a containment test.

It returns true if the left operand is contained in the right.

Testing for a value in an array

{% if 1 in [1, 2, 3] %}
{% endif %}

If your array is contained in a variable you could do something like this:

{% if 'value' in myVariable %}
{% endif %}

Testing for a value in a string

You can test to see if a string contains a specific substring.

{% if 'cd' in 'abcde' %}
{% endif %}

If your string is contained in a variable you can use do the following:

{% if 'string' in myStringVariable %}
{% endif %}

Negative tests

To perform a negative test you can use the not in operator.

{% if 1 not in [1, 2, 3] %}
{% endif %}

{% if 'string' not in 'This test text' %}
{% endif %}

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).

OperatorDescription
and

Returns true if the left and the right operands are both true. Alternate syntax to &&.

{% if 1 > 2 and 3 == 3 %}
{% endif %}

&&

Returns true if the left and the right operands are both true. Alternate syntax to and.

{% if 1 > 2 && 3 == 3 %}
{% endif %}

or

Returns true if the left or the right operand is true. Alternate syntax to ||.

{% if 1 > 2 or 3 == 3 %}
{% endif %}

||

Returns true if the left or the right operand is true. Alternate syntax to or.

{% if 1 > 2 || 3 == 3 %}
{% endif %}

Note, this is two pipe characters |

not

Negates a statement. It can be used with any of the tests. Below are a few examples.

{% if not 'Bear' ends with 'x' %}
{% endif %}

{% if 1 not in [1, 2, 3] %}
{% endif %}

{% if number is not even %}
{% endif %}

Math

Twig allows you to calculate with values.  The following operators are supported.

OperatorDescription
+

Adds two numbers together. If the values are not numbers they are cast as 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 an alternate format for dividing and then using the round filter.

*

Multiplies the left value with the right one.

{{ 2 * 2 }} = 4

**

Raises the left value to the power of the right value.

{{ 2 ** 3 }} = 8

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.

OperatorDescription
is

The is operator test a variable against a common expression.

{% if number is odd %}
{% endif %}

{% if foo.attribute is same as(false) %}
    the foo attribute really is false.
{% endif %}

{% if users is iterable %}
    {# users is an array #}
    {% for user in users %}
       Hello {{ user.name }}!
    {% endfor %}
{% else %}
    {# users is probably a string #}
    Hello {{ users }}!
{% endif %}

Negative tests

Tests can be negated by using the is not operator.

{% if count is not even %}
{% endif %}

See the Tests page for a full list of available tests.