Closed chrispella closed 2 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.
@chrispella Not sure if still interested, but added support in latest commits for <extend>
tag definition, documentation
Hi @dmulyalin I've been looking at this but can't get it to work. even with relative paths.
@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
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.
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
FYI - extend tag is now part of TTP starting from release 0.8.0
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.
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.
Wow, this is very interesting. I think this kind of feature may significantly increase template reusability.
@amyasnikov Added macro support for extend tag in release 0.9.0, feel free to test it at your own convenience.
sample extend tag macro usage in doco - https://ttp.readthedocs.io/en/latest/Extend%20Tag/Extend%20Tag.html#macro
@dmulyalin That's very cool, thanks! I'll check this out
Closing this one, please reopen if any further help needed.
Is there a way to include external templates in a template in order to avoid duplication?