twig tutorial - Ternary Operator and Null-Coalescing Operator in Twig - twig php - twig template
The ternary operator (?:)
- Support for the extended ternary operator was added in Twig 1.12.0.
{{ foo ? 'yes' : 'no' }}
Evaluates:
if foo echo yes else echo no
{{ foo ?: 'no' }}
or
{{ foo ? foo : 'no' }}
Evaluates:
if foo echo it, else echo no
{{ foo ? 'yes' }}
or
{{ foo ? 'yes' : '' }}
Evaluates:
if foo echo yes else echo nothing
The null-coalescing operator (??:)
{{ foo ?? 'no' }}
Evaluates:
Returns the value of foo if it is defined and not null, no otherwise