php - twig: IF 有多个条件

标签 php twig conditional-operator

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/

相关文章:

javascript - 与代码块比较确定大写和小写

php - Twig 中 Bundle 的相对路径

java - 三元从句中连续两个问号是什么意思?

php - 电子邮件帮助 - php mail() 或 SMTP

php - 防止 PHP 中的 session 冲突

symfony - 从 Twig 中的集合字段中获取值

php - 当数组反转时,几个 div 会移开,从而破坏布局

javascript - TypeScript 不考虑未定义的三元运算符

php - 排序顺序参数

php - 获取可见网址