我正在使用 Flask,我想要一個模板來呈現一組專案。
到目前為止一切正常,但是這些專案有一個顏色屬性,我想將其設定為背景顏色。
我認為我可以對我在 jinja-loop 中渲染的每個元素簡單地使用行內樣式,但是如果在字串中作業,我找不到解決方案:
{%for i in items%}
<div class="square-button" style="background: '{{ i.color if i.color is not none else '#000' }}';" onclick="itemClicked(this)" index="{{ loop.index }}"> ... </div>
{%endfor%}
有沒有辦法做到這一點?
提前致謝!
編輯:
我讓它與以下適應一起作業:
<div ... style="{{ 'background: ' data[i][k] if data[i][k] is not none else '#000' ';' }}">...</div>
可悲的是,這在 HTML 中被標記為錯誤。我想它并不是真的要那樣使用它,如果有一個干凈的解決方案,請賜教!:)
uj5u.com熱心網友回復:
嘗試這個
{%for i in items %}
<div class="square-button" {% if i.color %} style="background:{{ i.color}};" {% else %} style="background: #000;" {% endif %} onclick="itemClicked(this)" index="{{ loop.index }}"> ... </div>
{%endfor%}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/447269.html
上一篇:未能使用MSVC選擇正確的operator==而不是gcc/clang用于模板類
下一篇:模板Complex<T>的“未定義對`operator>>(std::istream&,Complex<int>&)的參考”
