2017-06-22 56 views
0

它會聽起來很愚蠢,但我不知道如何在我的樹枝模板中編寫這個三元條件。樹枝 - 三元條件運算符

{% for post in posts %} 
    <div class="news_text {{ loop.index is odd ? left : right }}"> 
    {{ post.content }} 
    </div> 
{% endfor %} 

有人能告訴我什麼是好的語法嗎? :-)

回答

0

你可以試着用另一種方式來做這件事。 根據結果集輸出向左或向右創建if和。

{% for post in posts %}  
    {% set output = "right" %} 
    {% if loop.index is odd %} 
     {% set output = "left" %} 
    {% endif %} 
    <div class="news_text {{ output }}">  
{% endfor %} 

但是,如果你想要做你的方式嘗試:

{% for post in posts %} 
    <div class="news_text {{ loop.index is odd ? "left" : "right" }}"> 
{% endfor %} 
+0

哦完美THX! :-) 我更喜歡第二種解決方案,因爲它更緊湊。我有一個使用ifs的「修復」,但它不是很漂亮^^ – Cellendhyll

+0

很酷,如果它對你有用 - 請務必接受我的回答 – Fiffe

+0

哦,我不知道我可以做到這一點^^ 完成: - ) – Cellendhyll