如何在Jinja 2模板中传递转义字符串变量

2024-09-30 16:40:25 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一项简单的任务 我想将颜色例如:“rgb(255255,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)\"")

在CHROME的HTML中,它的名称是color: "rgb(200,0)" 这种颜色不会出现。请帮帮我


Tags: 文件in文本mapfor颜色htmlrgb
1条回答
网友
1楼 · 发布于 2024-09-30 16:40:25
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)')

jinja 2模板:-

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

enter image description here

相关问题 更多 >