我正在做一个小工作板,每张工作都有小卡片。这些卡是从我的python脚本发送到使用flask / jinja的html的列表中动态创建的
jobs = [{'title':'fix car', 'description':'my car broke down', 'price':100}]
HTML示例
{% for item in jobs %}
<p> {{ item['title'] }} {{ item['description'] }} {{ item['price'] }} </p>
{% endfor %}
我想要的是能够使选定的卡突出或特色。所以在我的Python脚本中,作业来自我添加了二进制选项。 1 =特色,0 =正常
所以在我的HTML中,它看起来像这样:
{% for item in jobs %}
<p class='featured if item['featured']'> {{ item['title'] }} {{ item['description'] }} {{ item['price'] }}
{% endfor %}
所以如果它的特色键设置为1,它将获得一个新的CSS类。
CSS示例
.featured {
background-color: blue;
}
您可以使用内联条件来获得更短的结果:
<p class='{{"featured" if item["featured"] else "normal"}}'> {{ item['title'] }} {{ item['description'] }} {{ item['price'] }}</p>
但是,如果您不想包含非功能卡的类,可以使用通用的if
-else
语句:
{%if item["featured"]%}
<p class='featured'> {{ item['title'] }} {{ item['description'] }} {{ item['price'] }}</p>
{%else%}
<p> {{ item['title'] }} {{ item['description'] }} {{ item['price'] }}</p>
{%endif%}