我有以下 Jinja2 模板:
{% block body %}
{% for key in tree recursive %}
{% set outer_loop = loop %}
{% for subkey in tree[key] %}
{% if subkey == 'R' %}
{{ tree[key][subkey] }}
{% else %}
{{ outer_loop(dict([(subkey, tree[key][subkey])])) }}
{% endif %}
{% endfor %}
{% endfor %}
{% endblock body %}
where tree
是一个 Python 字典,例如:
tree = {"A": {"R": [1, 2, 3], "B": {"R": [4, 5, 6]}}}
and dict()
is the Python库函数 http://docs.python.org/2/library/stdtypes.html#dict.
问题是模板仅显示[1, 2, 3]
并不是[1, 2, 3][4, 5, 6]
正如预期的那样。
显然我误解了 Jinja 中递归的工作原理;有什么提示吗?