2014-12-03 70 views
1

我在模板10-20中有相同的if語句。枝條分隔if語句

例子:

{% if a == b %} 
    <div>text</div> 
{% endif %} 

other code 

{% if a == b %} 
    <span></span> 
{% endif %} 

other code 

{% if a == b %} 
    <div>text 2</div> 
{% endif %} 

,現在如果我需要改變我的條件,必須在幾個地方進行更改。

如何輕鬆區分這種情況並僅在一個地方更換?

回答

2

您可以保存條件導致一個變量:

{% set ab_cond = a == b %} 

{% if ab_cond %} 
    <div>text</div> 
{% endif %} 

other code 

{% if ab_cond %} 
    <span></span> 
{% endif %} 

other code 

{% if ab_cond %} 
    <div>text 2</div> 
{% endif %} 
-1

計算一次,將結果存儲爲一個變量,在if語句中使用該變量。

+0

您可以通過解釋如何做到這一點改進這個答案。 – 2014-12-05 23:14:22