2016-11-02 24 views
1

一個條件I通常具有相似的樣板代碼在嫩枝:裹內容通過元件滿足在嫩枝

{% if (somelongcond) %} 
<div> 
{% endif %} 
    <p>Some long content</p> 
{% if (somelongcond) %} 
</div> 
{% endif %} 

與上述的問題是,如果條件改變,也可以是維護惡夢,我還必須仔細查看匹配的if語句並查看條件是否相同。

另一種方法是這樣的:

{% if (somelongcond) %} 
    <div> 
     {% include 'content' %} 
    </div> 
{% endif %} 
{% include 'content' %} 

但這需要創建一個新的文件,它可以成爲一個爛攤子,如果我需要做很多次。

有沒有更好的方法來做到上述。

回答

0

這是一個有點短

{{ if somelongcond ? '<div>'|raw }} 
    <p>Some long content</p> 
{{ if somelongcond ? '</div>'|raw }} 

然後,如果重複相同的條件下,你也許可以將它設置爲你的文件的頂部,然後如果你需要改變它,你只需要做到這一點一旦。

{% if somelongcond %} 
    {% set cond = true %} 
{% else %} 
    {% set cond = false %} 
{% endif %} 

{{ if cond ? '<div>'|raw }} 
    <p>Some long content</p> 
{{ if cond ? '</div>'|raw }}