gpt4 book ai didi

php - twig: IF 有多个条件

转载 作者:IT老高 更新时间:2023-10-28 11:48:18 26 4
gpt4 key购买 nike

twig if 语句似乎有问题。

{%if fields | length > 0 || trans_fields | length > 0 -%}

错误是:

Unexpected token "punctuation" of value "|" ("name" expected) in 

我不明白为什么这不起作用,就像所有管道都丢失了 Twig 一样。

我试过了:

{% 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 ?

最佳答案

如果我没记错的话,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)) %}

关于php - twig: IF 有多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8388537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com