Passing a macro in rendering context so that the macro can be called dynamically #463
-
Is it possible to pass the name of a macro into the context while rendering and then allow the template to use the passed macro? I am looking for a dynamic way to render multiple views of the same data. I am implementing the views as macros as they can be called with parameters. I can currently do this by creating a template for every view and then calling the macro in that template. I would like to avoid having to create both a macro and then the template. It looks like I can call a function dynamically, but I have found no way to call a macro dynamically If this does not work, then I will likely implement everything in a template. It is not the end of the world, but it is slightly less reusable. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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:
{% from "./macros1.html" import macro1 %}
{% from "./macros2.html" import macro2 %} example.html: {% import "all-macros.html as macros %}
{{ macros[macro_name]() }} If the variable |
Beta Was this translation helpful? Give feedback.
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
:example.html:
If the variable
macro_name
is thenmacro2
it will invokemacro2
.