使用Symfony

时间:2017-11-07 13:07:15

标签: php arrays symfony twig

这个数组是通过发送到Symfony从我的php输出的。

    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
                )

        )

)

在.twig文件中,我无法组装正确显示的代码,它只显示数组的第一级和第二级,如何显示第三级和第四级?我的.twig看起来像这样:

{% 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 %} 

输出应该是这样的一个例子。

LocalizarPeças - &gt; GM(雪佛兰) - &gt;敏捷 - &gt; CAPO

但是就这样,我只能在这里用.twig / html显示它。

LocalizarPeças - &gt; GM(雪佛兰)

我需要有人帮助我理解我做错了什么,因为在上面的例子中,它仍然没有显示数组的第三级。我想我的循环不正确。

1 个答案:

答案 0 :(得分:2)

你没有循环正确的变量。在您的示例中,children未知,因为您尚未设置它。

你可以这样做:

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

或者

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

第二种方法是使用set将children变量分配给category.children。只有现在你可以迭代这些。

在你的模板上有4个或无限循环,直到没有更多的孩子f.e.您必须在模板中调用相同的树枝模板,如下所示: Category.html.twig

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

这将继续循环,直到所有类别循环及其子项,这是有效的,因为我将变量类别作为子项传递,然后将在下一次调用twig文件时使用。因此,孩子成为新的类别变量。