Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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> 
like image 244
Kamran Ahmed Avatar asked Nov 02 '13 06:11

Kamran Ahmed


People also ask

Is not equal to in Twig?

Twig != is a comparison operator. You can use != to compare any two things that php lets you compare, and what you get back is a boolean.


1 Answers

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> 
like image 63
adam187 Avatar answered Oct 05 '22 07:10

adam187