Skip to content Skip to sidebar Skip to footer

Jinja2: Macro Selecting Macro Or Dynamic Macro Calls

I have a list of namedtuples I'm going through, each slightly differing in rendering requirements, so I want to call the proper macro based on an attribute. What I have is this: {%

Solution 1:

You can create a Jinja2 filter which gets the Macro from the current context and then evaluates the Macro. The filter is:

@contextfilterdefcall_macro_by_name(context, macro_name, *args, **kwargs):
    return context.vars[macro_name](*args, **kwargs)

See the full answer here.

Post a Comment for "Jinja2: Macro Selecting Macro Or Dynamic Macro Calls"