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 |
---|---|
| | The pipe charactor | applies a filter. {{ profile.firstName|capitalize }} {% if item.itemName|lower == 'my name' %} {{ 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 %} You can do a similar thing with letters. {% for letter in 'a'..'z' %} The .. operator can take expressions (filters) at both sides {% for letters in 'a'|upper..'z'|upper %} |
~ | 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 formIf 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.
Operator | Description |
---|---|
== | Compares the left value equals the right value. {% if value1 == value2 %} 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 %} |
< | Compares the left value with the right value to see if the left value is less than the right value. {% if 3 < 4 %} |
<= | 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 %} |
> | Compares the left value with the right value to see if the left value is greater than the right value. {% if 4 > 2 %} |
>= | 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 %} |
starts with | Checks to see if a string starts with a certain value. {% if 'BranchCMS' starts with 'Bran' %} |
ends with | Checks to see if a string ends with a certain value. {% if 'BranchCMS' ends with 'CMS' %} |
matches | Use a regular expression to check a value. {% if phone matches '/^[\\d\\.]+$/' %} |
Containment
The in operator lets you see if the left value is contained in the right value.
Operator | Description |
---|---|
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] %} If your array is contained in a variable you could do something like this: {% if 'value' in myVariable %} Testing for a value in a stringYou can test to see if a string contains a specific substring. {% if 'cd' in 'abcde' %} If your string is contained in a variable you can use do the following: {% if 'string' in myStringVariable %} Negative testsTo perform a negative test you can use the not in operator. {% if 1 not in [1, 2, 3] %} {% if 'string' not in 'This test text' %} |
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 | Returns true if the left and the right operands are both true. Alternate syntax to &&. {% if 1 > 2 and 3 == 3 %} |
&& | Returns true if the left and the right operands are both true. Alternate syntax to and. {% if 1 > 2 && 3 == 3 %} |
or | Returns true if the left or the right operand is true. Alternate syntax to ||. {% if 1 > 2 or 3 == 3 %} |
|| | Returns true if the left or the right operand is true. Alternate syntax to or. {% if 1 > 2 || 3 == 3 %} 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' %} {% if 1 not in [1, 2, 3] %} {% if number is not even %} |
Math
Twig allows you to calculate with values. The following operators are supported.
Operator | Description |
---|---|
+ | 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.
Operator | Description |
---|---|
is | The is operator test a variable against a common expression. {% if number is odd %} {% if foo.attribute is same as(false) %} {% if users is iterable %} Negative testsTests can be negated by using the is not operator. {% if count is not even %} See the Tests page for a full list of available tests. |