# Twig ternary operators

# Operators

if foo echo yes else echo no

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

If foo echo it, else echo no:

{{ foo ?: 'no' }}

or

{{ foo ? foo : 'no' }}

If foo echo yes else echo nothing:

{{ foo ? 'yes' }}

or

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

Returns the value of foo if it is defined and not null, no otherwise:

{{ foo ?? 'no' }}

# Default

Returns the value of foo if it is defined(empty values also count), no otherwise:

{{ foo|default('no') }}
Date de modification: 11/5/2020, 7:46:02 PM