Деревья в PHP + Twig 2.0
Предположим, есть задачка, вывести дерево.
Под рукой PHP, а как view — Twig.
Пусть структура данных на backend будет такой:
$tree = [
[
'name' => 'foo',
'children' => [
[
'name' => 'bar',
'children' => [
[
'name' => 'baz',
'children' => [
],
],
[
'name' => 'baz',
'children' => [
],
],
]
]
]
]
];
Тогда простроить дерево можно так:
{% macro makeTree(node) %}
{% import _self as self %}
-
{% for child in node.children %}
{{ self.makeTree(child) }}
{% endfor %}
-
{% for node in tree %}
{{ makeTree(node) }}
{% endfor %}
Получится вложенный список из ul и li.