How can I refer to an user defined Enum type in a Jinja template?
- User
How can I refer to an user defined Enum type in a Jinja template?
- Assistant
In Jinja templates, you can refer to a user-defined Enum in the same way you would refer to any other variable. First, you need to pass the Enum to the template. Then, you can access the Enum values directly.
Here's an example:
Python code:
from enum import Enum from flask import render_template class Color(Enum): RED = 1 BLUE = 2 GREEN = 3 @app.route('/') def home(): return render_template('home.html', Color=Color)Jinja template(home.html):
{% if some_variable == Color.RED.value %} <p>The color is red.</p> {% elif some_variable == Color.BLUE.value %} <p>The color is blue.</p> {% elif some_variable == Color.GREEN.value %} <p>The color is green.</p> {% else %} <p>Unknown color.</p> {% endif %}
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.