开发者

Jinja2 - Given 2 templates (as strings) how to render one that extends the other?

开发者 https://www.devze.com 2023-01-23 09:17 出处:网络
I\'m making a simple script that works on Jinja2 templates. Right now it\'s just reading files in from disk manually, i.e. no Jinja Loaders. I have 2 strings (A and B), representing 2 templates. I wan

I'm making a simple script that works on Jinja2 templates. Right now it's just reading files in from disk manually, i.e. no Jinja Loaders. I have 2 strings (A and B), representing 2 templates. I want to make one template (B) inherit from the other (A), i.e. I have {% block body %}{% endblock %} in A, and I want to make the body block be the contents of B. How can I get开发者_开发技巧 the rendered output of this?

Normally I'd use {% extends 'filename' %} in B and it'd use the right one, however I don't have the filename (per se) for A.


Your best bet probably to use a different template loader. Take a look at DictLoader and FunctionLoader, or try your hand a writing your own template loader.

0

精彩评论

暂无评论...
验证码 换一张
取 消