Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Struggling to get values from an array

Started by Muut Archive 10 years ago · 7 replies · 694 views
10 years ago

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

YAML
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:

TWIG
{% 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...

10 years ago

Why are you using the same variable name for two different things?

TWIG
{% 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.

10 years ago

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?

10 years ago

As Perlkonig points out in his example, you generally never want to override a parent variable. In your example, you do:

TWIG
{% 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:

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

Ok cool thanks I see what you mean now. Cheers

10 years ago

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.

TWIG
{% 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:

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

Any ideas?

10 years ago

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:

TWIG
{% 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)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1354 9 years ago
Archive · by Muut Archive, 9 years ago
2 935 9 years ago
Archive · by Muut Archive, 9 years ago
2 4065 9 years ago
Archive · by Muut Archive, 9 years ago
1 2953 9 years ago
Archive · by Muut Archive, 9 years ago
3 1119 9 years ago