【发布时间】:2015-10-21 04:34:50
【问题描述】:
我希望能够应用 DRY 而不必在构建我的 jinja2 模板时重复自己。所以我希望能够从动态构造的变量名中引用 jinja2 模板中的变量,例如:
{% for a in [ 'a', 'b', 'c'] %}
{% set name = a + "_name" %}
{% set value = {{ name }} %}
hello there {{ value }}
{% endfor %}
我在 jinja 中的输入变量在哪里
a_name = 1
b_name = 2
c_name = 3
结果是
hello there 1
hello there 2
hello there 3
这可能吗?
我知道我只是将一个数据结构传入 jinja2 来做类似的事情,但我无权修改模板中的内容。
【问题讨论】: