Struggling to get values from an array

Hi, I have an array and I can’t work out how to get the values properly:

downloads:
    -
        text: 'Annual Implementation Plan'
        dl:
            user/pages/02.our-school/06.documentation/file-01.docx:
                name: file-01.docx
                type: application/vnd.openxmlformats-officedocument.wordprocessingml.documen t
                size: 321505
                path: user/pages/02.our-school/06.documentation/file-01.docx

If I do this I can get the invidual values inside the ‘dl’ array:

{% for item in page.header.downloads %}
{% for item in item.dl %}
   <a href="{{ item.path }}" class="item">{{ item.text }} {{ item.size }}</a>
{% endfor %}
{% endfor %}

But then I can’t get item.text because it’s outside the loop, in the loop above… thanks…

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:

<a href="{{ item.path }}" class="item">{{ download.text }} {{ item.size }}</a>

Ok cool thanks I see what you mean now. Cheers

I’m lost again on something here - any idea how to get the loop.first from the original loop whilst in the second loop? E.g.

{% for download in page.header.downloads %}
  {% for item in download.dl %}
	{% if loop.first %} something {% endif %}
    <a href="{{ item.path }}" class="item">{{ download.text }} {{ item.size }}</a>
  {% endfor %}
{% endfor %}

I’ve tried this but it doesn’t work:

{% if download.loop.first %} something {% endif %}

Any ideas?

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 %}

(untested)

Awesome, thank you