Adriaan
Adriaan

Reputation: 715

Colored text in Jinja2 template depending on condition in Python

I have the Jinja2 template

<!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>

in which I would like to give "I want this text in green!" the color green if value[2] == False and red otherwise.

How do I achieve this?

Upvotes: 4

Views: 4607

Answers (1)

tomasantunes
tomasantunes

Reputation: 945

You can use a condition to set a class on the table cell.

<td class="{{'red' if value[2] == True else 'green'}}">Text</td>

<style>
    .red {
        color: red;
    }

    .green {
        color: green;
    }
</style>

Upvotes: 3

Related Questions