2011-12-05 19 views
95

Sembra che abbia un problema con un'istruzione ramoscello.ramoscello: SE con più condizioni

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

L'errore è:

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

non riesco a capire il motivo per cui questo non funziona, è come se ramoscello è stato perso con tutti i tubi.

Ho provato questo:

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

ma se anche sicuro.

Poi provato questo:

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

E ancora non funziona, lo stesso errore ogni volta ...

Quindi ... che mi portano a una davvero semplice domanda: supporta Twig più condizioni SE?

risposta

229

se ricordo bene Ramoscello non supporta || e && operatori, ma richiede or e and da utilizzare, rispettivamente. Vorrei usare anche le parentesi per denotare le due affermazioni in modo più chiaro, anche se questo non è tecnicamente un requisito.

{%if (fields | length > 0) or (trans_fields | length > 0) %} 

Espressioni

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. 

Per operazioni più complesse, può essere meglio per avvolgere le singole espressioni tra parentesi per evitare confusione:

{% if (foo and bar) or (fizz and (foo + bar == 3)) %} 
+13

E naturalmente ho avuto alcuna possibilità di trovare che tabella meravigliosa e risparmio di tempo quando si guarda la documentazione IF: http://twig.sensiolabs.org/doc/tags/if.html Grazie per la soluzione! – FMaz008

+5

Tendono ad usare la wiki su github per documentare più a fondo il loro codice. Tale tabella proviene da [qui] (https://github.com/vito/chyrp/wiki/Twig-Reference) –

+14

Anche gli operatori fanno distinzione tra maiuscole e minuscole. O non funziona. – Acyra