Joe Scotto Joe Scotto - 1 year ago 73
PHP Question

Twig for loop stopping after two results (0, 1)

I have a loop that is getting passed the following array via a slim view in PHP:

'breadcrumbs' => array(
'path' => $breadcrumbs,
'directory' => $breadcrumbDirectory

are arrays.

I am using the following loop to display the contents via Twig.

<li><a href="/panel">Home</a></li>
{% for breadcrumb in breadcrumbs %}
<li><a href="/panel/{{[loop.index0] }}">{{ breadcrumbs.path[loop.index0] }}</a></li>
{% endfor %}

This code is working except it only displays the first 2 (0, 1) results. I am able to access indexes greater than 1 by calling
{{ breadcrumbs.path[2] }}
outside of the loop. Any help would be great, Thanks!

Answer Source

Your for loop is incorrect, you need to loop one of the two arrays inside of it :

<li><a href="/panel">Home</a></li>
    {% for path in breadcrumbs['path'] %}
        <li><a href="/panel/{{[loop.index0] }}">{{ path }}</a></li>
    {% endfor %}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download