I have hierarchical structure:
我有层次结构:
{
"content": "Header 1",
"name": "folder/name.txt",
"decendent": [
{
"content": "Header 2",
"name": "folder/subfolder/name.txt",
"decendent": null
},
{
"content": "Header 3",
"name": "folder/subfolder2/name.txt",
"decendent": [
{
"content": "Header 4",
"name": "folder/subfolder2/subsubfolder1/name.txt",
"decendent": null
}
... etc.
]
}
]
}
I have to unroll it using (as an example) this template:
我必须使用(作为示例)此模板展开它:
{% for key, value in list.items %}
<ul class="Container">
<li class="Node ExpandClosed">
<div class="Expand">
</div>
<div class="Content">
<a href="/help/pur/">
{{ key }}
</a>
</div>
{% for k, v in value.items %}
<ul class="Container">
<li class="Node ExpandClosed">
<div class="Expand">
</div>
<div class="Content">
<a href="/help/test/">
{{ k }}
</a>
</div>
{% for k1, v1 in v.items %}
<ul class="Container">
<li class="Node ExpandClosed">
<div class="Expand">
</div>
<div class="Content">
<a href="/help/test/">
{{ k1 }}
</a>
</div>
{% for k2, v2 in v1.items %}
<ul class="Container">
<li class="Node ExpandClosed">
<div class="Expand">
</div>
<div class="Content">
<a href="#" onclick="k2 = '{{ k2 }}'; changeText(k2)">
{{ k2 }}
</a>
</div>
</li>
</ul>
{% endfor %}
</li>
</ul>
{% endfor %}
</li>
</ul>
{% endfor %}
</li>
</ul>
{% endfor %}
I have to put the "name" attribute to "a href" tag, and between opening and closing tag extract the "content", for descendents of node I'd like to go through them recursively. Unfortunately, I have no idea how to perform it using django template language considering it's limitations.
我必须将“name”属性设置为“a href”标签,并在开始和结束标签之间提取“内容”,对于节点的后代,我希望递归地浏览它们。不幸的是,考虑到它的局限性,我不知道如何使用django模板语言来执行它。
Can you help me, please?
你能帮我吗?
1 个解决方案
#1
1
Django can use recursive templates. If you create a template called (for example) recurse.html
, and pass it a data
variable with your top-level dictionary:
Django可以使用递归模板。如果您创建一个名为(例如)recurse.html的模板,并使用您的*字典将其传递给数据变量:
<ul class="Container">
<li class="Node ExpandClosed">
<div class="Expand"></div>
<div class="Content">
<a href="{{data.name}}">
{{data.content}}
</a>
</div>
{% for item in data.decendent %}
{% include 'recurse.html' with data=item %}
{% endfor %}
</li>
</ul>
That should display for the first level, then re-call itself each time for it's decendents
with data
re-bound to the new sub-tree.
这应显示为第一级,然后每次为其后代重新调用自己,并将数据重新绑定到新的子树。
#1
1
Django can use recursive templates. If you create a template called (for example) recurse.html
, and pass it a data
variable with your top-level dictionary:
Django可以使用递归模板。如果您创建一个名为(例如)recurse.html的模板,并使用您的*字典将其传递给数据变量:
<ul class="Container">
<li class="Node ExpandClosed">
<div class="Expand"></div>
<div class="Content">
<a href="{{data.name}}">
{{data.content}}
</a>
</div>
{% for item in data.decendent %}
{% include 'recurse.html' with data=item %}
{% endfor %}
</li>
</ul>
That should display for the first level, then re-call itself each time for it's decendents
with data
re-bound to the new sub-tree.
这应显示为第一级,然后每次为其后代重新调用自己,并将数据重新绑定到新的子树。