dmulyalin / ttp

Template Text Parser
MIT License
350 stars 34 forks source link

include other templates #40

Closed chrispella closed 2 years ago

chrispella commented 3 years ago

Is there a way to include external templates in a template in order to avoid duplication?

dmulyalin commented 3 years ago

Hi,

To some extent yes. You can have multiple templates defined within single template as well as load templates in TTP object programmatically.

However, its not doable to include groups within templates similar to Jinja2 extend feature, best of what I can think of as a workaround is to divide your groups in templates - one group per template and add the to TTP object before parsing.

Just thought - TTP template is an XML document, meaning you can potentially use XMLEtree to load it and add more tags to it, but template has to have proper XML structure to load without errors.

dmulyalin commented 3 years ago

@chrispella Not sure if still interested, but added support in latest commits for <extend> tag definition, documentation

draggeta commented 3 years ago

Hi @dmulyalin I've been looking at this but can't get it to work. even with relative paths.

dmulyalin commented 3 years ago

@draggeta to confirm, have you installed ttp from github mater branch,? Also, this is example of how to use extend from ttp extend tag tests https://github.com/dmulyalin/ttp/blob/57c011f0168ba06692cb421cbaf199c57a2c9520/test/pytest/test_extend_tag.py#L25

draggeta commented 3 years ago

Ah, no, I pulled it from pypi. I assumed wrongly that it was already released. In that case I'll test it later with an install from master.

dmulyalin commented 3 years ago

Once git installed on your system, try this command to pull and install ttp from master:

python3 -m pip install git+https://github.com/dmulyalin/ttp

dmulyalin commented 3 years ago

FYI - extend tag is now part of TTP starting from release 0.8.0

amyasnikov commented 3 years ago

Hi @dmulyalin! I want to propose some little extension to the extend tag: the indent attribute. Example usage:

<extend template="/some/template.txt" indent="4"/>

As a result, 4 space characters will be prepended to all the lines in /some/template.txt It will be very useful in parsing juniper configs. For example we have interfaces config part and groups config part. Groups config may contain absolutely the same interfaces configuration but with bigger indentation. So, with indent attribute you may define interfaces config only once in separate file and then refer to that file multiple times with different value of indent attribute.

I think I can implement this feature by myself if you agree with accepting it into TTP.

dmulyalin commented 3 years ago

Sounds interesting, but probably more generic macro function reference would be better, where you can refer to any python function to pre process template before adding it to parent. Will have a look into it.

amyasnikov commented 3 years ago

Wow, this is very interesting. I think this kind of feature may significantly increase template reusability.

dmulyalin commented 2 years ago

@amyasnikov Added macro support for extend tag in release 0.9.0, feel free to test it at your own convenience.

dmulyalin commented 2 years ago

sample extend tag macro usage in doco - https://ttp.readthedocs.io/en/latest/Extend%20Tag/Extend%20Tag.html#macro

amyasnikov commented 2 years ago

@dmulyalin That's very cool, thanks! I'll check this out

dmulyalin commented 2 years ago

Closing this one, please reopen if any further help needed.