简体   繁体   中英

Array in array Twig

A small Twig question regarding array in array access. 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. 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.

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. Eventually stumbled into it on the page source, and that's how I got to the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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