3

I am trying to use a simple if statement in Twig. However it doesn't seem to be working properly. The variable emailActivation is certainly being loaded into Twig properly as can be seen from the first line of the output in all cases.

However the if statement is behaving very oddly - for example it works as expected when the variable is false and being tested against 1 (case 2) but not when it is false and tested against 'false' (case 1).

Conversely when the variable is true the if statement works when the variable is tested against 'true' (case 3) but not when tested against 1 (case 4).

I only show the tests against 1 as a workaround I attempted, really I think the problem lies in (case 1). (case 5) and (case 4) suggest the numbers are being evaluated as boolean.

I also thought it could perhaps be an issue to do with caching, but to my knowledge I have set Twig up not to use a cache. Even if Twig is caching the problem still occurs on the first page load and when the browser is restarted. I have tried in IE and Firefox though again I don't think the browser is really relevant to the problem which lies in Twig itself as far as I can tell.

Thanks in advance for any assistance!

Case 1 emailActivation = false

Twig code

{{ emailActivation }} {% if emailActivation == true %} feature disabled {% else %} other content {% endif %} 

Output
false
feature disabled

Case 2 emailActivation = false

Twig code

{{ emailActivation }} {% if emailActivation == 1 %} feature disabled {% else %} other content {% endif %} 

Output
false
other content

Case 3 emailActivation = true

Twig code

{{ emailActivation }} {% if emailActivation == true %} feature disabled {% else %} other content {% endif %} 

Output
true
feature disabled

Case 4 emailActivation = true

Twig code

{{ emailActivation }} {% if emailActivation == 1 %} feature disabled {% else %} other content {% endif %} 

Output
true
other content

Case 5 emailActivation = true

Twig code

{{ emailActivation }} {% if emailActivation == 0 %} feature disabled {% else %} other content {% endif %} 

Output
true
feature disabled

4
  • emailActivation is a string ? Because I see false or true in the result of {{ emailActivation }} [instead of nothing or 1 [or something else] first time i see this]] Commented Dec 23, 2015 at 12:27
  • @Put12co22mer2 That makes a lot of sense! I can't test to confirm atm but will tomorrow. It is odd that there is a different result in case 4 and 5 though if it is a string. Thanks Commented Dec 23, 2015 at 12:35
  • Yes It was just a thinking not an answer, and still strange I don't understand why case 5... Commented Dec 23, 2015 at 12:41
  • 1
    @Put12co22mer2 You are correct it was a string. I'm new to stack exchange but I think you need to post that as an answer for me to accept it? Commented Dec 24, 2015 at 0:11

2 Answers 2

1

One detail make think that emailActivation could be a string.

The output of {{ emailActivation }} is false or true, but I think that a boolean return something else (~ nothing or 1 ~).

The fact to test the condition as being a string should resolve the problem.

One way will be (for simply a string, not a big content):

{{ emailActivation }} {{ (emailActivation == 'true') ? 'feature disabled' : 'other content' }} 

Friendly

Sign up to request clarification or add additional context in comments.

Comments

1

try same as it is equivalent to === in PHP http://twig.sensiolabs.org/doc/tests/sameas.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.