I have a big if-elif
structure that calls specific macros based on the names of dynamically called variables, like this:
{% macro select_macro() %}
do stuff
{%- if context.key == 'value1' %}
{{ macro1 }}
{%- elif context.key == 'value2' %}
{{ macro2 }}
{%- elif context.key == 'value3' %}
{{ macro3 }}
{% endif %}
do more stuff
{% endmacro %}
To have a more “pythonic” code that calls the needed child macros dynamically, I’ve tried using @pass_context
using the example shown in the second related question below (which still uses deprecated @contextfilter
), but I’m getting errors stating that the value
object is not attached to a Session. I have an app factory
structure, and the @pass_context
function was defined in the decorators.py
. The Flask documentation on @pass_context
is very economical, to say the least. Any advice or a functioning example will be greatly appreciated.
Related questions:
this
“eval” statement in Jinja2 template
and this
How can I indirectly call a macro in a Jinja2 template?