简体   繁体   English

数组中的树枝

[英]Array in array Twig

A small Twig question regarding array in array access. 关于数组访问中的数组的一个小Twig问题。 I have the following script, which is essentially listing categories, and then the subcategories associated to each category. 我有以下脚本,该脚本实质上是列出类别,然后是与每个类别关联的子类别。

{% for category in categories %}

    <li>
        <a href="#">{{ category.name }}</a>

        {% if category.subcategories|length > 0 %}

        {% set subcategories  = category.subcategories %}

        <ul>
            {% for subcategory in subcategories %}

            <li>
                <a href="#">{{ subcategory.name }}</a>
            </li>

            {% endfor %}
        </ul>
        {% endif %}
    </li>

{% endfor %}

The script above does not display subcategories and I have no clue as to why. 上面的脚本没有显示子类别,我也不知道为什么。 The main problem is the fact that the for loop is never accessed. 主要的问题是for循环从未被访问过。 Dumping the subcategories variable right after setting it reveals the expected, that it holds the correct array, with one element. 设置后立即转储子类别变量将显示预期的结果,即它拥有一个元素的正确数组。

Any ideas? 有任何想法吗?

Dumping the subcategories variable reveals: 转储子类别变量显示:

array (size=2)
  0 => 
    array (size=2)
      'id' => int 1
      'name' => string 'Dolls' (length=5)
  1 => 
    array (size=2)
      'id' => int 2
      'name' => string 'Test' (length=4)

Problem was actually not related to Twig at all in this case, but to a class that was applied to the li item that held everything together, that only displayed the content when a specific class was applied to it. 在这种情况下,问题实际上与Twig根本不相关,而是与应用于将所有内容组合在一起的li项目的类有关,该类仅在将特定类应用于该项目时才显示内容。

In a very bizarre way, the node simply got deleted by a JS when the class was not applied, so firebug would not pick it up. 以一种非常奇怪的方式,当不应用该类时,该节点只是被JS删除,因此firebug不会将其拾取。 Eventually stumbled into it on the page source, and that's how I got to the solution. 最终在页面源代码中偶然发现了它,这就是我寻求解决方案的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM