Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean logic doesn't work as I expect in twig

 {{ dump(extend) }}

Result:

  boolean false

And when I want to make this:

{% if extend is true %}
    {% extends 'WelcomePageBundle:Default:welcome_page.html.twig' %}
{% endif %}

It doesn't work. Why?

Error:

The test "false" does not exist in FOSUserBundle:ChangePassword:changePassword.html.twig at line 1
like image 509
Lukas Lukac Avatar asked Feb 26 '13 12:02

Lukas Lukac


2 Answers

It has to be either {% if extend %} — because extend is already a boolean — or {% if extend == true %}. is is used for tests; not for comparison.

like image 186
Elnur Abdurrakhimov Avatar answered Nov 02 '22 06:11

Elnur Abdurrakhimov


You need to use empty test:

Test empty checks if a variable is empty (null, false, empty array, or empty string).

{% if extend is not empty %}
    ...
{% endif %}

Take a look at the list of available tests as well as logic operators from the official Twig documentation.

like image 40
gremo Avatar answered Nov 02 '22 08:11

gremo