首页 > 解决方案 > 如何在 Jinja 2 模板中传递转义字符串变量

问题描述

这是一个简单的任务,我想将颜色例如:“rgb(255,255,0)”传递给 html 文件,以便可以更改文本颜色。

我在 html 文件中使用了这个片段

``{% for i in range(0,len) %}
new mapboxgl.Marker({
    color: {{ colors[i]|safe }}
    }).setLngLat([
        {{ lang[i] }}, 
        {{ lat[i] }}])
    .addTo(map);

    {%endfor%}

我通过以下方式传递颜色列表:

colors=[]
maxValue = max(totalConfimedList)
for item in totalConfimedList:
    print(item)
    # colors.append(code_map(int(item),0,int(maxValue),0,255))
    if int(item) >255:
        colors.append("\'rgb(255,0,0)")
        continue
    colors.append("\"rgb("+item+"0,0)\"")

在 HTML IN CHROME 中,它即将到来,因为color: "rgb(200,0)" 颜色未到来。请帮我

标签: pythonflaskjinja2

解决方案


colors = []
totalConfimedList = ['123', '255', '256', '375', '367']
maxValue = max(totalConfimedList)
for item in totalConfimedList:
    print(item)
    # colors.append(code_map(int(item),0,int(maxValue),0,255))
    if int(item) > 255:
        colors.append('rgb(255,0,0)')
        continue
    colors.append('rgb('+item+',0,0)')

神社 2 模板:-

{% for i in colors %}
color: [{{ i|safe }}]
{%endfor%}

在此处输入图像描述


推荐阅读