-1

This is continuing from a previous post here. My twig file below can go two levels and using for loops but I have much more children within the children. The object contains an array of arrays and is in a hierarchical order. I am looking for a recursive solution if possible. It may be also helpful but not necessary that I am using bootstrap.

Twig:

{% extends 'CompanyMyBundle::base.html.twig' %}

{% block body -%}
    <h1>Org list</h1>

    <table class="records_list">
        <thead>
            <tr>
                <th>name</th>
                <th>parentid</th>
                <th>id</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
        {% for entity in entities %}
            <tr>
                <td><a href="{{ path('org_show', { 'id': entity.Id }) }}">{{ entity.Name }}</a></td>
               <td>{{ entity.ParentId }}</td>
                <td>{{ entity.Id }}</td>
                <td>
                    <ul>
                        <li>
                            <a href="{{ path('org_show', { 'id': entity.Id }) }}">show</a>
                        </li>
                        <li>
                            <a href="{{ path('org_edit', { 'id': entity.Id }) }}">edit</a>
                        </li>
                    </ul>
                </td>
            </tr>
            {% for child in entity.children %}
            <tr>
                <td><a href="{{ path('org_show', { 'id': child.Id }) }}">{{ child.Name }}</a></td>
                <td>{{ child.ParentId }}</td>
                <td>{{ child.Id }}</td>
                <td>
                <ul>
                    <li>
                        <a href="{{ path('org_show', { 'id': child.Id }) }}">show</a>
                    </li>
                    <li>
                        <a href="{{ path('org_edit', { 'id': child.Id }) }}">edit</a>
                    </li>
                </ul>
                </td>
            </tr>
            {% endfor %}
        {% endfor %}
        </tbody>
    </table>

        <ul>
        <li>
            <a href="{{ path('org_new') }}">
                Create a new entry
            </a>
        </li>
    </ul>
    {% endblock %}
Community
  • 1
  • 1
shayster01
  • 214
  • 3
  • 17

1 Answers1

1

Fairly easy. You could just {{ include }} the template file for each node. Something like this:

Node.html.twig

<tr>
    <td><a href="{{ path('org_show', { 'id': entity.Id }) }}">{{ entity.Name }}</a></td>
   <td>{{ entity.ParentId }}</td>
    <td>{{ entity.Id }}</td>
    <td>
        <ul>
            <li>
                <a href="{{ path('org_show', { 'id': entity.Id }) }}">show</a>
            </li>
            <li>
                <a href="{{ path('org_edit', { 'id': entity.Id }) }}">edit</a>
            </li>
        </ul>
    </td>
</tr>

{# Recursively print all children nodes #}
{% for child in entity.children %}
    {{ include("Node.html.twig", {'entity': child}) }}
{% endfor %}

And then in your main template:

{% for entity in entities %}
    {{ include("Node.html.twig", {'entity': entity}) }}
{% endfor %}

This is just a rough example, but you can probably see the idea behind it. It would be smart to think about possible recursion depths. The deeper you go, the greater the chance of overflowing the stack.

Hope this helps...

dsh
  • 12,037
  • 3
  • 33
  • 51
Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85
  • Key "children" for array with keys "145, 153" does not exist in src/Company/MyBundle/Resources/views/Org/node.html.twig at line 20 It looks like it is having issues with parents without parentid. These two have a parentid=null. @Jovan – shayster01 Aug 17 '15 at 17:41
  • 1
    I added {% if entities.children is defined %} around it and got rid of the error but it only returns the top two entries. – shayster01 Aug 17 '15 at 17:56
  • In the node.html.twi change the recursive function from {% for child in entities.children %} to {% for child in entity.children %} – shayster01 Aug 17 '15 at 18:48