Skip to main content

Cheatsheet

Twig provides a rich set of operators and syntax for templating that makes writing dynamic web pages a breeze. Here's a quick reference to some of the most commonly used Twig operators to help you streamline your development workflow.

Operators

if foo echo yes else echo no

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

If foo echo it, else echo no:

{{ foo ? foo : 'no' }}

If foo echo yes else echo nothing:

{{ 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') }}