0

I have array like this:

array(
  'id' => 1,
  'children' => array(
    'id' => 5,
    'children' =>
    array(
      'id' => 6,
      'children' => 'none',
    ),
    array(
      'id' => 8,
      'children' => array(...),
    )
  )
)

with php code can call Recursive Function to echo it. but in twig i can't call Recursive Function.

{% for category in categories %}
    <div class="category">
      <div class="id">{{ category.id }}</div>
    </div>
{% endfor %}

my code just print first level of array and not other!

J Maze
  • 11
  • 2
  • is this what you are looking for ? https://stackoverflow.com/questions/8326482/how-to-render-a-tree-in-twig – Nek May 18 '18 at 13:24
  • you could include the same view again so that the behavior is the same as in your recursive function. Like in this [answer](https://stackoverflow.com/a/8467939/2401386) – Blauharley May 18 '18 at 13:28

1 Answers1

0

You can with blocks:

{% set foo = { 'hello': { 'hello': { 'hello': 'swagg' }, 'foo': 'bar' } } %}


{% block test %}
<ul>
    {% for keyOfFoo, f in foo %}
    <li>
        {{ keyOfFoo }}
        {% if f is iterable %}
            {% with { foo: f } %}
            {{ block('test') }}
            {% endwith %}
        {% endif %}
    </li>
    {% endfor %}
</ul>
{% endblock test %}

Here is live demo: https://twigfiddle.com/r35pae

But there is also many other options. Like include loop. Or embedded.

Nek
  • 2,715
  • 1
  • 20
  • 34