FORSMILE
JA
Twig2021/02/09

[Twig] A Summary of Available Operators

The operators +, -, /, %, //, *, and ** are available.

Back to Blog

ARITHMETIC OPERATORS

The operators +, -, /, %, //, *, and ** are available.

Brief Explanation

css
+: 足し算  //  {{ 1 + 1 }} is 2.
-: 引き算  //  {{ 3 - 2 }} is 1.
/: 除算(割り算)  //  {{ 1 / 2 }} is {{ 0.5 }}.
*: 乗算(掛け算)  //  {{ 2 * 2 }} is 4.
%: 除算の余り  //  {{ 11 % 7 }} is 4.
//: 除算後、四捨五入された整数を返す  //  {{ 20 // 7 }} is 2, {{ -20  // 7 }} is -3
**: 累乗、(左側に対して右側の数値分)  //   {{ 2 ** 3 }} is 8.

LOGICAL OPERATORS

The operators `and`, `or`, `not`, and `()` are available. Use them in lowercase.

COMPARISON OPERATORS

Common comparison operators like `==`, `!=`, `<`, `>`, `>=`, `<=` are available.

The strict equality operator `===` is not available. Use `is same as(value)` instead.

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

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

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

The `matches` operator mentioned above is also introduced in the following article. It's useful when you want to compare with regular expressions, for example.

OTHER OPERATORS

`in`: Checks if a value is contained

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

You can also use `not` to check if a value is *not* contained.

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

`is`: Performs a test specified on the right side

This is frequently used in Twig. Below are examples of actual tests.

javascript
// constant : 定数との比較
{% if product.status is constant('PRODUCT::PUBLISHED') %}
    exactly the same as PRODUCT::PUBLISHED
{% endif %}

// difined : 定義されているかどうか
{% if foo is defined %}
{% endif %}

// null : nullかどうか
{% if foo is null %}
{% endif %}

// empty : 変数や配列が空かどうか
{% if foo is empty %}
{% endif %}

// divisible by() : 指定した数で割り切れるかどうか
{% if loop.index is divisible by(3) %}
{% endif %}

// iterable : 変数が配列かオブジェクトであることをチェックする
{% if foos is iterable %}
{% endif %}

// same as() : 厳密に同じかどうか
{% if foo is same as('test') %}
{% endif %}

// even,odd : 偶数、奇数かどうか
{% if foo is even %}
{% endif %}

`|`: Applies a filter

css
{{ name|lower }}

`..`: Creates a sequence based on the values before and after the operator

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

`~`: Converts values to strings and concatenates them

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

`?:`: Ternary Operator

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

Simply put, if `foo` is true, it returns "yes"; if `foo` is false, it returns "no".

`??`: Null Coalescing Operator

css
{{ foo ?? 'no' }}

If `foo` is not null and is defined, it returns the value of `foo`. Otherwise, it returns "no".

`#{expression}`: Allows you to interpolate an expression within a string.

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

RECOMMENDED TWIG BOOKS

I haven't personally read any books specifically dedicated to Twig, but I found some on Amazon Kindle.

📦
Amazon で関連書籍・ツールを検索
PHP Twig template engine
Amazonで探す →(アソシエイトリンク)