Joe Scotto Joe Scotto - 3 months ago 8
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
)


Both
$breadcrumbs
and
$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/{{ breadcrumbs.directory[loop.index0] }}">{{ breadcrumbs.path[loop.index0] }}</a></li>
{% endfor %}
</li>


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

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/{{ breadcrumbs.directory[loop.index0] }}">{{ path }}</a></li>
    {% endfor %}
</li>