-
In discussion #896 the use of Jinja template macros is adviced - and works really well btw. for complex templates. I was just wondering if there's a way to pass all Copier variables, both from the questionnaire and the ContextHook extension, to such a macro. So instead of # macro
{% macro default(var1 = '', var2 = '', var2 = '', ..., varN = '') %}
...
# template
{% import "path/to/my/macro" as mymacro %}
<% mymacro.default(var1 = copier_var1, var2 = copier_var2, ..., varN = copier_varN) %> it could be something like for macros relying a lot on Copier variables: # macro
{% macro default(...) %}
...
# template
{% import "path/to/my/macro" as mymacro %}
<% mymacro.default(<pass all copier variables somehow>) %> |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
I ended up using "models" and the # macros/macro_x.py
{% macro default(model = {}) %}
do_something with {% model["key1"] %}
{% endmacro %} # macros/macro_y.py
{% macro default(model = {}) %}
do_something with {% model["key1"] %}
{% endmacro %} # context.py
contextCustom = {
...
}
contextModels = {
# MyMacro X
"__mymacro_x_model" : {
"key1" : context["key1"],
"key2" : context["key2"],
"key3" : context["key3"],
"key4" : context["key4"],
},
# MyMacro Y
"__mymacro_x_model" : {
"key1" : context["key1"],
"key2" : context["key2"],
"key5" : context["key8"],
"key8" : contextCustom["key8"],
}
}
return contextCustom | contextModels # myfile.x
{% import "macros/macro_x.j2" as mymacro %}
{% mymacro.default(model = __mymacro_x_model %} # myfile.y
{% import "macros/macro_y.j2" as mymacro %}
{% mymacro.default(model = __mymacro_y_model %} |
Beta Was this translation helpful? Give feedback.
-
Maybe you're looking for include? |
Beta Was this translation helpful? Give feedback.
-
I didn't have
to achieve
But this should be possible using
I'll test it ASAP. Thanks for pointing that out. |
Beta Was this translation helpful? Give feedback.
-
Ok, the solution is much easier: |
Beta Was this translation helpful? Give feedback.
Ok, the solution is much easier:
{% import "path/to/my/macro.j2" as mymacro with context %}