2017-08-21 133 views
0

我剛剛開始學習樹枝,我真的被困在這個愚蠢的小問題。串聯但這似乎並沒有工作(eigenKleurInput最終將是一個值):樹枝:連接字符串變量

{% set eigenKleurInput = "acefbf" %} 
{% set customBackgroundColorInline = 'style=background-color: #' ~ eigenKleurInput %} 

輸出變量 「customBackgroundColorInline」 是一個div裏面放:

<section {{ customBackgroundColorInline }}> 

所需的輸出將

<section style="background-color: #xxx"> 

非常感謝!

回答

0

如果我正確理解你的問題,問題是關於編碼字符:如果你在你的代碼分支渲染中添加"作爲&quot;

在這種情況下,你應該使用raw filter如下:

{% set eigenKleurInput = "acefbf" %} 
{% set customBackgroundColorInline = 'style="background-color: #' ~ eigenKleurInput ~ '"' %} 


<section {{ customBackgroundColorInline|raw }}> 

所以輸出將是:

<section style="background-color: #acefbf"> 

你可以在this working twigfiddle

希望這有助於在線試用