Trying to make the code DRYer by nesting templates like this:
试着通过这样的嵌套模板来制作代码烘干机:
base = Template('''
- alert:
cluster: {{cluster}}
role: {{role}}
slack: {{slack}}
''')
alert = Template('''
{% include base %}
description: Critical {{role}} system load
threshold: xxx-yyy-zzz
''')
print alert.render(cluster='cluster1', slack='alerts', role='database')
The above does not work, getting the exception:
上面的不工作,得到一个例外:
File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 989, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 754, in handle_exception
reraise(exc_type, exc_value, tb)
File "<template>", line 2, in top-level template code
TypeError: no loader for this environment specified
Please advise.
请建议。
1 个解决方案
#1
1
You are merely not passing the reference to base
when rendering alert
template.
在呈现警报模板时,您只是没有将引用传递给base。
>>> print alert.render(cluster='cluster1', slack='alerts', role='database', base=base)
################ just pass this reference of `base` ^
- alert:
cluster: cluster1
role: database
slack: alerts
description: Critical database system load
threshold: xxx-yyy-zzz
#1
1
You are merely not passing the reference to base
when rendering alert
template.
在呈现警报模板时,您只是没有将引用传递给base。
>>> print alert.render(cluster='cluster1', slack='alerts', role='database', base=base)
################ just pass this reference of `base` ^
- alert:
cluster: cluster1
role: database
slack: alerts
description: Critical database system load
threshold: xxx-yyy-zzz