Skip to content

Passing a macro in rendering context so that the macro can be called dynamically #463

Answered by mitsuhiko
CaliViking asked this question in Q&A
Discussion options

You must be logged in to vote

Unfortunately passing actual macros to another template is not supported today due to technical limitations: #329

As a workaround you could import the macros into a meta template and then call them by name:

all-macros.html:

{% from "./macros1.html" import macro1 %}
{% from "./macros2.html" import macro2 %}

example.html:

{% import "all-macros.html as macros %}
{{ macros[macro_name]() }}

If the variable macro_name is then macro2 it will invoke macro2.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@CaliViking
Comment options

@CaliViking
Comment options

Answer selected by mitsuhiko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants