twig: IF 具有多个条件
看来我对树枝 if 语句有问题。
{%if fields | length > 0 || trans_fields | length > 0 -%}
错误是:
Unexpected token "punctuation" of value "|" ("name" expected) in
我不明白为什么这不起作用,就像树枝和所有管道一起丢失一样。
我已经尝试过这个:
{% set count1 = fields | length %}
{% set count2 = trans_fields | length %}
{%if count1 > 0 || count2 > 0 -%}
但 if 也失败了。
然后尝试了这个:
{% set count1 = fields | length > 0 %}
{% set count2 = trans_fields | length > 0 %}
{%if count1 || count2 -%}
它仍然不起作用,每次都出现同样的错误...
所以...这让我想到一个非常简单的问题: Twig 是否支持多个条件 IF ?
It seem I have problem with a twig if statement.
{%if fields | length > 0 || trans_fields | length > 0 -%}
The error is:
Unexpected token "punctuation" of value "|" ("name" expected) in
I can't understand why this doesn't work, it's like if twig was lost with all the pipes.
I've tried this :
{% set count1 = fields | length %}
{% set count2 = trans_fields | length %}
{%if count1 > 0 || count2 > 0 -%}
but the if also fail.
Then tried this:
{% set count1 = fields | length > 0 %}
{% set count2 = trans_fields | length > 0 %}
{%if count1 || count2 -%}
And it still doesn't work, same error every time ...
So... that lead me to a really simple question: does Twig support multiple conditions IF ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没记错的话 Twig 不支持
||
和&&
运算符,但需要or
和and
> 分别使用。我还会使用括号来更清楚地表示这两个语句,尽管这在技术上不是必需的。表达式
对于更复杂的操作,最好将各个表达式括在括号中以避免混淆:
If I recall correctly Twig doesn't support
||
and&&
operators, but requiresor
andand
to be used respectively. I'd also use parentheses to denote the two statements more clearly although this isn't technically a requirement.Expressions
For more complex operations, it may be best to wrap individual expressions in parentheses to avoid confusion:
对多个条件使用
!=
对我来说也不起作用,但是==
确实有效 - 实际上这看起来像是一个错误。可能的解决办法。
Using
!=
for multiple conditions does not work for me either, however==
does work - indeed this seems like a bug.Possible work around.