веточка: ЕСЛИ с несколькими условиями


Похоже, у меня проблема с оператором twig 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 несколько условий, ЕСЛИ?

Author: dreftymac, 2011-12-05

1 answers

Если я правильно помню, Twig не поддерживает операторы || и &&, но требует использования or и and соответственно. Я бы также использовал круглые скобки для более четкого обозначения двух утверждений, хотя технически это не является обязательным требованием.

{%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)) %}
 250
Author: Ben Swinburne, 2016-10-26 15:55:51