ветка: если с несколькими условиями


кажется, у меня есть проблема с утверждением 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 -%}

но если тоже не получится.

потом попытался это:

{% set count1 = fields | length > 0 %}
{% set count2 = trans_fields | length > 0 %}
{%if count1 || count2 -%}

и он по-прежнему не работает, та же ошибка каждый раз ...

так... это приводит меня к очень простому вопросу: поддерживает ли Twig несколько условий, если ?

1 102

1 ответ:

если я правильно помню, ветка не поддерживает || и && операторы, но требуется 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)) %}