Symfony2 Twig无限子深度 [英] Symfony2 Twig unlimited child depth
问题描述
我有一个自联接表,其中每个文件夹都有一个父表,并且此表的深度是无限的.一个文件夹可以有另一个文件夹作为父文件夹,而对深度没有限制.
I have a self-joining table where each folder has a parent, and the depth of this is unlimited. One folder can have another folder as a parent, no restriction on the depth.
今天,我的代码看起来像这样,我正在寻找一种方法,可以根据需要进行更深入的挖掘,而无需对每一步进行硬编码,也许有一种方法可以定义带有循环的twig函数,该循环可以调用自身在循环中的每一轮?
Today my code looks like this, and I am looking for a way of digging down as deep as it needs without hard-coding each step down, is there perhaps a way to define a twig function with a loop, that calls itself on each round in the loop?
<select id='parent' name='container'>
<option value='none'>No parent</option>
{% for folder in folders %}
<option value='{{ folder.id }}'>{{ folder.name }}</option>
{% for folder in folder.children %}
<option value='{{ folder.id }}'> {{ folder.name }}</option>
{% endfor %}
{% endfor %}
</select>
推荐答案
您需要一个单独的文件呈现选项,该选项以递归方式包含自身:
You need a separate file rendering options that recursively includes itself:
<select>
<option value="none">No parent</option>
{% include 'options.html.twig' with {'folders': folders, 'level': 0} %}
</select>
options.html.twig
:
{% for folder in folders %}
<option value="{{ folder.id }}">
{% for i in range(0, level) %} {% endfor %}
{{ folder.name }}
</option>
{% include 'options.html.twig' with {'folders': folder.children, 'level': level + 1} %}
{% endfor %}
我是在这里编写此代码的,所以不要指望它是正确的,但是它足以让您理解.
I wrote this code right here, so don't expect it to be correct, but it should be enough to give you the idea.
这篇关于Symfony2 Twig无限子深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!