Symfony2 Twig unlimited child depth

后端 未结 2 766
刺人心
刺人心 2021-01-01 04:49

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.<

相关标签:
2条回答
  • 2021-01-01 05:00

    This has to be done using recursion. I've never tested it with twig but you could develope a mechanism where you recursively including a template.

    So your current template would include itself within the loop until a specific condition is reached. So you need some kind of if clause in your inner loop.

    Good luck ;)

    0 讨论(0)
  • 2021-01-01 05:02

    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) %}&nbsp;{% 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.

    0 讨论(0)
提交回复
热议问题