在Jinja2中动态使用模板 [英] Dynamic use of templates in Jinja2
问题描述
python字典列表 l = [a,b,c,...,n ]
列表中的每个元素都是一个Python字典,看起来像这样:
d = {}
d ['type'] = 5
d ['content'] ='somestring'
现在我想把所有字典作为主模板中的一个列表。然而,每个字典的内容应该由一个子模板来呈现。用来渲染内容的模板应该由字典的类型变量来定义。
使用Jinja2可以实现的任何提示(我正在使用它通过烧瓶,如果有帮助..)
谢谢! 如果有人需要它:
{%for d in dicts%}
{%set template = d.type + '.html'%} {%include template%}
{%endfor%}
然后在模板中,您可以像这样访问内容:
{{d.content}}
感谢freenode的#pocoo频道donri!
I have to following scenario:
a python list of python dictionaries l = [a,b,c,...,n]
each element of the list is a python dictionary that looks something like this:
d = {}
d['type'] = 5
d['content'] = 'somestring'
Now i want all dictionaries as a list in in a main template. However each dictionary's content should be rendered by a child template. Which template to use to render the content should be defined by the type variable of the dictionary.
Any hints on how this can be accomplished using Jinja2 (I'm using it via Flask if that helps..)
Thanks!
If anyone needs it:
{% for d in dicts %}
{% set template = d.type + '.html' %} {% include template %}
{% endfor %}
then in the template you can access the content like so:
{{ d.content }}
Thanks to donri from the #pocoo channel on freenode !
这篇关于在Jinja2中动态使用模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!