Include macro definitions from a file
volodymyrprokopyuk opened this issue · 6 comments
Hi,
What is the idiomatic way to include
macro
s defined in a separate file into the current template?
Use case. I'm using a template that extends
a master template and defines block
s from it. I've defined a set of macro
s in a separate file and I want all the macro
s to be available in the template. The file structure is
master-termplate.nwt
defines overall document structure viablock
stemplate.nwt
defines specific content for eachblock
macros.nwt
definesmacro
s to be used in thetemplate.nwt
Currently I managed to define all macro
s at the top of the master-termplate.nwt
, but I'd like to separate them in a dedicated file macros.nwt
and somehow include macros.nwt
into the template.nwt
. How this can be done?
Thank you very much,
Vlad
You can use importnwt
to import the macro file.
See this test: https://github.com/enthus1ast/nimja/blob/master/tests/basic/test_proc_import.nim
Hi,
I've used the {% importnwt "macros.nwt" %}
in the master-template.nwt
and it works fine! Note: the {% importnwt "macros.nwt" %}
in the template.nwt
does not work, which, as far as I understand, is the correct and expected behavior.
Thank you very much for the advacie!
I'll have a look at the importnwt from the child template. Do you have an error message?
Edit: do you had this stmt in a block? I can imagine that it does not work yet when it's not defined in a block. I'll have a look how jinja2 or twig handles this case, will report back.
Reopened, until clarified if this should work from a child
We do not have the sematics to do otherwise, so closed for now,
importnwt works good from the extended template and from childs in blocks.
Hi @enthus1ast,
Sorry for the late response. Please find below my responses to your questions
- I
importnwt "macros.nwt"
at the top level in the childtemplate.nwt
as it was the most straightforward approach to me. It did not work and it did not emit any error messages- Advice. In this case an error message should be emitted to warn about incorrect use of the
importnwt
- Nice to have. It could be useful to be able to
importnwt "macros.nwt"
at the top-level of the childtemplate.nwt
where themacros.nwt
are actually used en every block. Currently it works if theimportnwt "macros.nwt"
is at the top level of the parentmaster-template.nwt
. If you have multiple blocks in the childtemplate.nwt
it is not very intuitive toimportnwt "macros.nwt"
in one of them to get access tomacros.nwt
in every child block, or to be forced toimportnwt "macros.nwt"
in each child block.
- Advice. In this case an error message should be emitted to warn about incorrect use of the
Thank you!