【问题标题】:If is not variable Twig condition如果不是可变的 Twig 条件
【发布时间】:2014-04-09 10:16:57
【问题描述】:

我需要在twig中设置一些条件,所以我有这个:

{% if app.session.get('campaignVersion') is not null and is not '4.4d'}
...
{% elseif app.session.get('campaignVersion') is null or '4.4d' %}
...
{% endif %} 

但是我在语法和逻辑上有错误,也许它必须有一个标准运算符,例如!=,我做错了什么?谢谢帮助。

【问题讨论】:

  • 顺便说一句,我会在控制器中进行这样的计算,在这种情况下为您留下一个更可重用的模板。

标签: php templates twig conditional-statements


【解决方案1】:

Twig 不是人类语言解释器 :-)

Twig 无法隐含地知道and is not '4.4d' 中的主题是谁。

尝试:

{% if app.session.get('campaignVersion') is not null and app.session.get('campaignVersion') != '4.4d' %}

或者为了更好的可读性:

{% if app.session.get('campaignVersion') not in [null, '4.4d'] %}

【讨论】:

  • Unexpected token "string" of value "4.4d" ("name" expected)
  • 编辑了第一个例子。 "is not" 接受测试(以name 标记开头),而不是字符串。我将其替换为!=。不管怎样,你应该看看第二个例子。
【解决方案2】:

第一行末尾缺少一个 %

【讨论】:

  • 好的,这是个问题,但我也有一些逻辑问题,或者条件定义......
  • 我认为它们是 elseif 的第二个条件中的问题,如果你说不为空(这没关系),那么你只需输入 '4.4d' 而不说是否:是或不是
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-21
  • 2021-03-07
  • 1970-01-01
  • 2017-08-04
  • 2019-04-11
  • 2023-03-09
  • 1970-01-01
相关资源
最近更新 更多