How to render a tree in Twig

前端 未结 6 1966
灰色年华
灰色年华 2020-11-30 19:20

I would like to render a tree with an undetermined depth (children of children of children, etc.). I need to loop through the array recursively; how can I do this in Twig?

6条回答
  •  我在风中等你
    2020-11-30 19:37

    Took flu's answer and modified it a little:

    {# Macro #}
    
    {% macro tree(items) %}
        {% import _self as m %}
            {% if items %}
            
      {% for i in items %}
    • {{ i.title }} {{ m.tree(i.items) }}
    • {% endfor %}
    {% endif %} {% endmacro %} {# Usage #} {% import 'macros.twig' as m %} {{ m.tree(items) }}

提交回复
热议问题