[Twig] Summary of available operators

Commonly used operators are also available in Twig.
If you are familiar with the program, you can write it by speculation.
I would like to summarize it roughly.

Arithmetic operator

You can use the operators of +, -, /, %, //, *, **.

easy explanation

+: Addition  //  {{ 1 + 1 }} is 2.
-: Subtraction  //  {{ 3 - 2 }} is 1.
/: Division  //  {{ 1 / 2 }} is {{ 0.5 }}.
*: Multiply  //  {{ 2 * 2 }} is 4.
%: The remainder of the division  //  {{ 11 % 7 }} is 4.
//: Returns a rounded integer after division  //  {{ 20 // 7 }} is 2, {{ -20  // 7 }} is -3
**: raising the left value to the power of the right value  //   {{ 2 ** 3 }} is 8.

 

Logical operator

It is used for conditional branching (if statement).
and, or, not,() can be used. Use in lowercase.
&& and || can’t use.

Comparison operator

==, !=, <, >, >=, <=Common comparison operators can be used.
The exact equality operator === cannot be used. Use is same as(value) instead.
!== is is not same as(value).
Other available comparisons are listed below.

{% if 'foo' starts with 'f' %}
{% endif %}

{% if 'bar' ends with 'r' %}
{% endif %}

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

The above matches is also introduced in the following article. This is useful when you want to compare with a regular expression.
[Twig] Summary of how to use if statement

Other operators

“In”: Check if the value is included

// returns true
{{ 1 in [1, 2, 3] }}
{{ 'cd' in 'abcde' }}

You can also use not to check that it is not included.

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

“Is”: Do the test specified on the right

I often use it in Twig. Below is a sample of the actual test.

// constant : Comparison with constants
{% if product.status is constant('PRODUCT::PUBLISHED') %}
    exactly the same as PRODUCT::PUBLISHED
{% endif %}

// difined : Whether it is defined
{% if foo is defined %}
{% endif %}

// null : Whether null
{% if foo is null %}
{% endif %}

// empty : Whether variables or arrays are empty
{% if foo is empty %}
{% endif %}

// divisible by() : Whether it is divisible by the specified number
{% if loop.index is divisible by(3) %}
{% endif %}

// iterable : Check that the variable is an array or an object
{% if foos is iterable %}
{% endif %}

// same as() : Whether they are exactly the same
{% if foo is same as('test') %}
{% endif %}

// even,odd : Whether it is even or odd
{% if foo is even %}
{% endif %}

“|”: Apply filter

{{ name|lower }}

 

“..”: Create a sequence based on the values before and after the operator

{% set numbers = 1..5 %}
{{ dump(numbers) }}
// result
array:5 [▼
  0 => 1
  1 => 2
  2 => 3
  3 => 4
  4 => 5
]

 

“~”: Convert values to strings and concatenate them

It is used when combining variables.

{% set name = 'tester' %}
{{ "Hello " ~ name ~ "!" }}
// result
Hello tester!

 

“?:”: Ternary operator

{{ foo ? 'yes' : 'no' }}

Simply put, it returns “yes” if foo is true. Returns “no” if foo is false.

“??”: null coalescing operator

{{ foo ?? 'no' }}

Returns the value of “foo” if foo is defined rather than null. Otherwise, it returns “no”.

“# {Expression}”: You can insert an expression by interrupting between strings.

{{ "foo #{1 + 2} baz" }}
// result
foo 3 baz

 
The above is a summary of the operators that can be used in Twig.

| D-NET

| D-NET

[…] [Twig] Summary of available operators […]

Comments are closed.