twig Basic template syntax Conditional blocks


Example

Parts of template can be displayed conditionally. If statement is used for this purpose. It's similar to if statement in programing languages. Contents of block are executed/displayed if an expression evaluates to true.

{% if enabled == false %}
    Disabled
{% endif %}

Disabled will be displayed only when enabled will be equal false.

Multiple branches can be created using elseif and else.

{% if temperature < 10 %}
    It's cold
{% elseif temperature < 18 %}
    It's chilly
{% elseif temperature < 24 %}
    It's warm
{% elseif temperature < 32 %}
    It's hot
{% else %}
    It's very hot
{% endif %}