Boolean logic doesn't work as I expect in twig
Asked Answered
C

2

5
 {{ 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
Cyrillus answered 26/2, 2013 at 12:5 Comment(0)
D
10

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.

Distill answered 26/2, 2013 at 12:14 Comment(4)
ofc i tried but the result is: An exception has been thrown during the compilation of a template ("Node "1" does not exist for Node "Twig_Node"Cyrillus
The error you're getting is not related to the question; you're getting it for other reasons. See Conditional Inheritance for insights.Distill
hm... ah ok will try do it somehow else .. and can you please check on this: i am ultra desperate here :/ #15075330 thx!Cyrillus
watch out as well because 'true' has to be in lowercase (i.e. inProduction == True will fail)Hypersonic
C
4

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.

Choreographer answered 26/2, 2013 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.