twig tutorial - Conditional blocks in Twig - twig block - twig if - twig php - twig template



Conditional blocks

  • 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 %}

Related Searches to twig tutorial - Conditional blocks in Twig