我正在尝试使用jinja2渲染这样的对象:
{
"template": "It was the {{foo}} of times, it was the {{bar}} of times.",
"params": {
"foo" : "best"
"bar" : "worst"
},
"styling": {
"params": {
"foo": {
"classes": ["my_css_class_A"],
}
"bar": {
"classes": ["my_css_class_B"],
}
}
}
}
对此:
It was the <span class="my_css_class_A">best</span> of times, it was the <span class="my_css_class_B">worst</span> of times.
在Jinja模板中是否有一种优雅的方法来执行此操作,还是我需要编写自己的python函数?
后面的故事可能/可能无济于事:以前,我一直在使用python字符串模板来呈现这样的对象:
my_bullet_point = {
"template": "It was the {{foo}} of times, it was the {{bar}} of times.",
"params": {
"foo" : "best"
"bar" : "worst"
},
}
渲染的线(在更大的jinja模板中)过去看起来像这样:
<li>{{ my_bullet_point | render_template }}</li>
render_template在哪里:
from string import Template as pTemplate
def render_template(template):
return pTemplate(template["template"]).substitute(template["params"])
现在,我需要添加样式并仍然在Jinja中渲染。