0

This array is output from my php by sending to Symfony.

    Array
(
    [categories] => Array
        (
            [0] => Array
                (
                    [name] => Localizar Peças
                    [children] => Array
                        (

                            [0] => Array
                                (
                                    [name] => GM (Chevrolet) 
                                    [href] => /index.php?route=product/category&path=130_64
                                    [children2] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [name] => Agile
                                                    [href] => /index.php?route=product/category&path=130_64_68
                                                    [children3] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [name] => Capô
                                                                    [href] => /index.php?route=product/category&path=130_68_76
                                                                )

                                                            [1] => Array
                                                                (
                                                                    [name] => Condensador
                                                                    [href] => /index.php?route=product/category&path=130_68_82
                                                                )

                                                            [2] => Array
                                                                (
                                                                    [name] => Farol
                                                                    [href] => /index.php?route=product/category&path=130_68_79
                                                                )

                                                            [3] => Array
                                                                (
                                                                    [name] => Lanterna
                                                                    [href] => /index.php?route=product/category&path=130_68_80
                                                                )

                                                            [4] => Array
                                                                (
                                                                    [name] => Painel Frontal
                                                                    [href] => /index.php?route=product/category&path=130_68_78
                                                                )

                                                            [5] => Array
                                                                (
                                                                    [name] => Para-choques
                                                                    [href] => /index.php?route=product/category&path=130_68_84
                                                                )

                                                            [6] => Array
                                                                (
                                                                    [name] => Para-lama
                                                                    [href] => /index.php?route=product/category&path=130_68_77
                                                                )

                                                            [7] => Array
                                                                (
                                                                    [name] => Radiador
                                                                    [href] => /index.php?route=product/category&path=130_68_81
                                                                )

                                                            [8] => Array
                                                                (
                                                                    [name] => Ventoinha
                                                                    [href] => /index.php?route=product/category&path=130_68_83
                                                                )

                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [name] => Astra
                                                    [href] => /index.php?route=product/category&path=130_64_69
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [2] => Array
                                                (
                                                    [name] => Celta
                                                    [href] => /index.php?route=product/category&path=130_64_72
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [3] => Array
                                                (
                                                    [name] => Classic
                                                    [href] => /index.php?route=product/category&path=130_64_70
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [4] => Array
                                                (
                                                    [name] => Corsa
                                                    [href] => /index.php?route=product/category&path=130_64_71
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [5] => Array
                                                (
                                                    [name] => Cruze
                                                    [href] => /index.php?route=product/category&path=130_64_73
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [6] => Array
                                                (
                                                    [name] => Montana
                                                    [href] => /index.php?route=product/category&path=130_64_74
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [7] => Array
                                                (
                                                    [name] => Prisma
                                                    [href] => /index.php?route=product/category&path=130_64_75
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                        )

                                )


                        )

                    [column] => 1
                    [href] => /index.php?route=product/category&path=130
                )

        )

)

In the .twig file I am not able to assemble a code for correct display, it displays only the first and second level of the array, how do I display the third and fourth level as well? My .twig looks like this:

{% if categories %}
<div class="container">
  <nav id="menu" class="navbar">
    <div class="navbar-header"><span id="category" class="visible-xs">{{ text_category }}</span>
      <button type="button" class="btn btn-navbar navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"><i class="fa fa-bars"></i></button>
    </div>
    <div class="collapse navbar-collapse navbar-ex1-collapse">
      <ul class="nav navbar-nav">
        {% for category in categories %}
        {% if category.children %}
        <li class="dropdown"><a href="{{ category.href }}" class="dropdown-toggle" data-toggle="dropdown"><b>
          <div style="font-size: 15px;">{{ category.name }}</div>
          </b></a>
          <div class="dropdown-menu">
            <div class="dropdown-inner"> {% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}
              <ul class="list-unstyled">
                {% for child in children %}
                <li><a href="{{ child.href }}">{{ child.name }}</a>
                  <ul class="dropdown-menu sub-menu dropdown-inner">
                    {% for child2 in children2 %}
                    <li> <a href="{{ child2.href }}" >{{ child2.name }}</a> // level 3 here -> no works </li>
                    {% endfor %}
                  </ul>
                </li>
                {% endfor %}
              </ul>
              {% endfor %} </div>
            <a href="{{ category.href }}" class="see-all">{{ text_all }} {{ category.name }}</a> </div>
        </li>
        ....
        {% else %}
        <li><a href="{{ category.href }}">{{ category.name }}</a></li>
        {% endif %}
        {% endfor %}
      </ul>
    </div>
  </nav>
</div>
{% endif %} 

An example of how output should be would be like this.

Localizar Peças -> GM (Chevrolet) -> Agile -> Capô

But the way it is, I can only display it in .twig / html here.

Localizar Peças -> GM (Chevrolet)

I need someone to help me understand what I'm doing wrong, because, in the example above, it still does not show the third level of the array. I think I'm looping incorrectly.

2
  • Ok, I removed the duplication and left only this, which is easier to understand. Thank you. Commented Nov 7, 2017 at 16:01
  • Thank you for that Commented Nov 7, 2017 at 16:02

1 Answer 1

2

You're not looping the right variables. In your example children is not known as you didn't set it yet.

You could do as followed:

{% for category in categories%}
 {% for child in category.children %}
  {{ child.name }}
 {% endfor %}
{% endfor %}

Or

{% for category in categories %}
 {% set children = category.children %}
 {% for child in children %}
  {{ child.name }}
 {% endfor %}
{% endfor %}

The second method is using set to assign the children variable to category.children. Only now you can iterate over these.

To have 4 or unlimited loops over your templates until no more children f.e. you'll have to call the same twig template from within the template like this: Category.html.twig

{% for child in category.children %}
  {% include "Category.html.twig" with { "category": child } %}
{% endfor %}

This will continue looping until all categories are looped and their children, this works because I'm passing the variable category as the child which will then be used in the next call of the twig file. So the child becomes the new category variable.

Sign up to request clarification or add additional context in comments.

1 Comment

I must be doing something wrong yet. I can not display the third level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.