pfalcon / utemplate

Micro template engine in Python with low memory usage, designed for Pycopy, a minimalist Python dialect, but also compatible with other Pythons.
https://github.com/pfalcon/pycopy
76 stars 8 forks source link

Reuse of included templates #14

Open doc-hex opened 4 years ago

doc-hex commented 4 years ago

I was surprised to learn the {% include ... %} does not generate a separate function. If I include the same file into two other templates, the generated code does not get reused but is duplicated. There also does not seem to be a way to pass variables (or context) into an included file, which creates a problem for my usual nav-bar style menus that highlight the currently active page.

pfalcon commented 4 years ago

I was surprised to learn the {% include ... %} does not generate a separate function.

Well, it does, except this function is nested within the function for the main template. The original idea for {% include "static.file" %} was to work similar to how for example C's #include works.

Later, support for dynamic includes was added, and they behave in a way you want - they import include'd template from a Python module, to which it was compiled. But oops, I see that an example of that lied in my local workcopy, uncommitted. It now is: https://github.com/pfalcon/utemplate/blob/master/examples/include_dyn.tpl.

Well, maybe now handling of the static includes should also be switched to this way, but I'd prefer to see a real compelling usecase before making changes.

pfalcon commented 4 years ago

There also does not seem to be a way to pass variables (or context) into an included file

Actually, there's, but again, there was no example of that. Now pushed: https://github.com/pfalcon/utemplate/commit/1de3078feceb48b1ca1e720272d79071b8c8c8b1

doc-hex commented 4 years ago

Include arguments: perfect, very useful.

As for my use case: any site with more than one page will have lots of boilerplate in the . I don't want to repeat that in the flash memory of the product.