首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Jinja2模板中的彩色文本取决于Python语言中的条件

Jinja2模板中的彩色文本取决于Python语言中的条件
EN

Stack Overflow用户
提问于 2019-06-26 21:55:10
回答 1查看 1.5K关注 0票数 2

我有Jinja2模板

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html lang="en">
    <body>
        <table style="width:30%">
            {% for key, value in forces.get_max_allowable_force_per_edge_exceeded_or_not().items() %}
                <tr>
                    <td>{{key}}</td>
                    <td>{{"{:+.2e}".format(value[0])}} * N</td>
                    <td>{{"I want this text in green!" if value[2] == False else "I want this text in red!"}}</td>
                </tr>
            {% endfor %}
        </table>
    </body>
</html>

如果是value[2] == False,我想给"I want this text in green!"颜色是绿色,否则是红色。

我该如何实现这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-26 23:10:25

您可以使用条件在表单元格上设置类。

代码语言:javascript
运行
复制
<td class="{{'red' if value[2] == True else 'green'}}">Text</td>

<style>
    .red {
        color: red;
    }

    .green {
        color: green;
    }
</style>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56774542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档