Search code examples
symfonytwigconditional-statements

Nested if else in Twig


Is there any way to implement nested if else functionality in twig? I have tried the following but it isn't working:

<body 
        {% if page|default('login') == 'login' %} 
              class="login"
        {% else if( page == 'other') %}
              class="login"
        {% else %}
              class="noclass"
        {% endif %}>
</body>

Solution

  • elseif needs to be single word tag/keyword and expression shouldn't have parenthesis same as if expression.

    http://twig.sensiolabs.org/doc/tags/if.html

    <body
        {% if page|default('login') == 'login' %}
              class="login"
        {% elseif page == 'other' %}
              class="login"
        {% else %}
              class="noclass"
        {% endif %}>
    </body>