树枝:如果有多个条件 [英] twig: IF with multiple conditions

查看:139
本文介绍了树枝:如果有多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我的树枝if语句有问题.

It seem I have problem with a twig if statement.

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

错误是:

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.

我已经尝试过了:

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

它仍然不起作用,每次都出现相同的错误...

And it still doesn't work, same error every time ...

那么...这使我想到了一个非常简单的问题:Twig是否支持多种条件(IF)?

So... that lead me to a really simple question: does Twig support multiple conditions IF ?

推荐答案

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

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 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)) %}

这篇关于树枝:如果有多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆