twig: IF 具有多个条件

发布于 2024-12-19 20:33:56 字数 672 浏览 1 评论 0原文

看来我对树枝 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡水深流 2024-12-26 20:33:56

如果我没记错的话 Twig 不支持 ||&& 运算符,但需要 orand > 分别使用。我还会使用括号来更清楚地表示这两个语句,尽管这在技术上不是必需的。

{%if ( fields | length > 0 ) or ( trans_fields | length > 0 ) %}

表达式

Expressions can be used in {% blocks %} and ${ expressions }.

Operator    Description
==          Does the left expression equal the right expression?
+           Convert both arguments into a number and add them.
-           Convert both arguments into a number and substract them.
*           Convert both arguments into a number and multiply them.
/           Convert both arguments into a number and divide them.
%           Convert both arguments into a number and calculate the rest of the integer division.
~           Convert both arguments into a string and concatenate them.
or          True if the left or the right expression is true.
and         True if the left and the right expression is true.
not         Negate the expression.

对于更复杂的操作,最好将各个表达式括在括号中以避免混淆:

{% if (foo and bar) or (fizz and (foo + bar == 3)) %}

If I recall correctly Twig doesn't support || and && operators, but requires or and and to be used respectively. I'd also use parentheses to denote the two statements more clearly although this isn't technically a requirement.

{%if ( fields | length > 0 ) or ( trans_fields | length > 0 ) %}

Expressions

Expressions can be used in {% blocks %} and ${ expressions }.

Operator    Description
==          Does the left expression equal the right expression?
+           Convert both arguments into a number and add them.
-           Convert both arguments into a number and substract them.
*           Convert both arguments into a number and multiply them.
/           Convert both arguments into a number and divide them.
%           Convert both arguments into a number and calculate the rest of the integer division.
~           Convert both arguments into a string and concatenate them.
or          True if the left or the right expression is true.
and         True if the left and the right expression is true.
not         Negate the expression.

For more complex operations, it may be best to wrap individual expressions in parentheses to avoid confusion:

{% if (foo and bar) or (fizz and (foo + bar == 3)) %}
梦忆晨望 2024-12-26 20:33:56

对多个条件使用 != 对我来说也不起作用,但是 == 确实有效 - 实际上这看起来像是一个错误。

可能的解决办法。

{% if key not in ['string1', 'string2'] %}
// print something
{% endif %}

Using != for multiple conditions does not work for me either, however == does work - indeed this seems like a bug.

Possible work around.

{% if key not in ['string1', 'string2'] %}
// print something
{% endif %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文