Lukas Lukac
Lukas Lukac

Reputation: 8376

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

Upvotes: 6

Views: 16074

Answers (2)

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44851

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.

Upvotes: 11

gremo
gremo

Reputation: 48487

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.

Upvotes: 4

Related Questions