2013-10-26 25 views
2

我想在我的樹枝用於循環使用if語句 - 如果對象(變量)包含一個元素(行) - 然後這個HTML代碼片斷,嫩枝 - 如果對象包含一個元素

這裏就是我正在嘗試,

{% for course in courses %} 
    <a href="{{ course.courseLink }}"class="courses"> 
     <div class="picture"><img src="{{ asset('/bundles/dproc/assets/images/courses-  example.jpg') }}" alt="news-1" title="news-1" /></div> 
     <div class="title"><h2>{{ course.courseTitle }}</h2></div> 
     <div class="info"> 
     {{ course.courseContent }} 
     </div> 
    </a> 
{% endfor %} 

此刻courses只包含一個元素。我的任務是添加一個div元素,如果它只包含一個元素。

  • 我怎麼能在樹枝上做到這一點?

回答

3

您可以使用此功能檢查數組的length

{% if courses|length == 1 %} 
    {# print div#} 
{% endif %} 
+0

非常感謝您的解決方案 –