Why are you using the same variable name for two different things?
{% for download in page.header.downloads %}
{% for item in download.dl %}
<a href="{{ item.path }}" class="item">{{ download.text }} {{ item.size }}</a>
{% endfor %}
{% endfor %}
Also, I don’t have time to test anything right now, but I’m pretty sure you’ll run into problems with dl as currently defined. Without the hyphen, YAML creates an object, not an array.
Hi, thanks that works and now I understand it, why it works. However what variable name am I using for two different things? and why wouldn’t the ‘dl’ variable work the same as any variable name?
As Perlkonig points out in his example, you generally never want to override a parent variable. In your example, you do:
{% for item in page.header.downloads %}
{% for item in item.dl %}
Which renders the initial item inaccessible in the nested loop. Perlkonig’s naming is necessary and proper because you want to access both the array from the initial loop and from the nested loop in the code:
The special Twig loop variable is only valid in the within the most recent loop. If you want to catch loop.first in the outer loop and use that information in the inner loop, you need to store that value earlier:
{% for download in page.header.downloads %}
{% set isfirstdl = false %}
{% if loop.first %}
{% set isfirstdl = true %}
{% endif %}
{% for item in download.dl %}
{% if isfirstdl %} something {% endif %}
<a href="{{ item.path }}" class="item">{{ download.text }} {{ item.size }}</a>
{% endfor %}
{% endfor %}