2011-12-05 14 views
95

ऐसा लगता है कि मुझे कथन के साथ समस्या है।टवीग: यदि कई स्थितियों के साथ

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

और यह अभी भी काम नहीं करता है वही त्रुटि हर बार ...

तो ... मुझे एक बहुत आसान सवाल करने के लिए नेतृत्व कि: टहनी का समर्थन करता है कई स्थितियों अगर?

उत्तर

229

अगर मैं सही ढंग याद टहनी || और && ऑपरेटरों का समर्थन नहीं करता है, लेकिन आवश्यकता है 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)) %} 
+13

और निश्चित रूप से मुझे लगता है कि खोजने का कोई मौका नहीं था IF दस्तावेज़ों को देखते समय अद्भुत और समय-बचत तालिका: http://twig.sensiolabs.org/doc/tags/if.html समाधान के लिए धन्यवाद! – FMaz008

+5

वे गिटूब पर विकी का उपयोग अपने कोड को अधिक अच्छी तरह से दस्तावेज करने के लिए करते हैं। वह तालिका [यहां] से आती है (https://github.com/vito/chyrp/wiki/Twig- संदर्भ) –

+14

इसके अलावा ऑपरेटर केस संवेदनशील होते हैं। या काम नहीं करता है। – Acyra

संबंधित मुद्दे