Twig IF语句不工作

Twig IF语句不工作,twig,Twig,我试图在Twig中使用一个简单的if语句。然而,它似乎不能正常工作。在所有情况下,从输出的第一行可以看出,变量emailActivation确实正确地加载到了Twig中 但是if语句的行为非常奇怪-例如,当变量为false并根据1进行测试时(情况2),if语句按预期工作,但当它为false并根据“false”(情况1)进行测试时,if语句则不工作 相反,当变量为true时,if语句在针对“true”(情况3)测试变量时有效,但在针对1(情况4)测试时无效 我仅将针对1的测试显示为我尝试的解决方法

我试图在Twig中使用一个简单的if语句。然而,它似乎不能正常工作。在所有情况下,从输出的第一行可以看出,变量emailActivation确实正确地加载到了Twig中

但是if语句的行为非常奇怪-例如,当变量为false并根据1进行测试时(情况2),if语句按预期工作,但当它为false并根据“false”(情况1)进行测试时,if语句则不工作

相反,当变量为true时,if语句在针对“true”(情况3)测试变量时有效,但在针对1(情况4)测试时无效

我仅将针对1的测试显示为我尝试的解决方法,实际上我认为问题在于(案例1)。(案例5)和(案例4)表明这些数字被计算为布尔值

我还认为这可能是一个与缓存有关的问题,但据我所知,我已设置Twig不使用缓存。即使Twig正在缓存,问题仍然会在加载第一页时以及重新启动浏览器时发生。我曾经尝试过IE和Firefox,但我认为就我所知,浏览器与Twig本身的问题并不相关

提前感谢您的帮助

案例1emailActivation=false

细枝代码

{{ emailActivation }}
{% if emailActivation == true %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 1 %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == true %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 1 %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 0 %}
    feature disabled
{% else %}
    other content
{% endif %}
输出

功能禁用

案例2emailActivation=false

细枝代码

{{ emailActivation }}
{% if emailActivation == true %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 1 %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == true %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 1 %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 0 %}
    feature disabled
{% else %}
    other content
{% endif %}
输出

其他内容

案例3emailActivation=true

细枝代码

{{ emailActivation }}
{% if emailActivation == true %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 1 %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == true %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 1 %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 0 %}
    feature disabled
{% else %}
    other content
{% endif %}
输出
正确
功能禁用

案例4emailActivation=true

细枝代码

{{ emailActivation }}
{% if emailActivation == true %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 1 %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == true %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 1 %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 0 %}
    feature disabled
{% else %}
    other content
{% endif %}
输出
正确
其他内容

案例5emailActivation=true

细枝代码

{{ emailActivation }}
{% if emailActivation == true %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 1 %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == true %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 1 %}
    feature disabled
{% else %}
    other content
{% endif %}
{{ emailActivation }}
{% if emailActivation == 0 %}
    feature disabled
{% else %}
    other content
{% endif %}
输出
正确

功能已禁用

请尝试与相同的它在PHP中等效为===
一个细节让人觉得emailActivation可能是一个字符串

{emailActivation}
的输出是
false
true
,但我认为布尔值返回其他值(~nothing或
1
~)

将条件作为字符串进行测试应该可以解决问题

一种方法是(对于简单的字符串,而不是大内容):


友好的

电子邮件激活是字符串吗?因为我在
{{emailActivation}}
[而不是我第一次看到的什么或1[或其他东西]]@Put12co22mer2的结果中看到false或true,这很有意义!我不能测试确认自动取款机,但明天可以。奇怪的是,在案例4和案例5中有一个不同的结果,尽管它是一个字符串。谢谢,这只是一个想法而不是一个答案,而且还是很奇怪,我不明白为什么案例5…@Put12co22mer2你是对的,这是一个字符串。我是stack exchange的新手,但我认为您需要发布此消息作为我接受它的答案?