enthus1ast/nimja

Include macro definitions from a file

volodymyrprokopyuk opened this issue · 6 comments

Hi,

What is the idiomatic way to include macros defined in a separate file into the current template?

Use case. I'm using a template that extends a master template and defines blocks from it. I've defined a set of macros in a separate file and I want all the macros to be available in the template. The file structure is

  • master-termplate.nwt defines overall document structure via blocks
  • template.nwt defines specific content for each block
  • macros.nwt defines macros to be used in the template.nwt

Currently I managed to define all macros 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 child template.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 child template.nwt where the macros.nwt are actually used en every block. Currently it works if the importnwt "macros.nwt" is at the top level of the parent master-template.nwt. If you have multiple blocks in the child template.nwt it is not very intuitive to importnwt "macros.nwt" in one of them to get access to macros.nwt in every child block, or to be forced to importnwt "macros.nwt" in each child block.

Thank you!