简体   繁体   中英

Twig template - access array within array

Just started playing around with twig templates and i've ran into an issue. I got an array i'm looping over and extracting data from, within this array i've got another array (images) that I need to access, problem is I can't seem to get it to work.

Here's the feed i've got

0 => 
    array (size=8)
      'id' => int 1
      'url' => string 'http://localhost' (length=16)
      'image' => string '8cgbfenx2n1.png' (length=15)
      'type' => string 'image' (length=5)
  1 => 
    array (size=10)
      'id' => int 17
      'images' => 
        array (size=3)
          0 => string 'xjv5y4asev2.png' (length=15)
          1 => string 'kwwss8f6r34.gif' (length=15)
          2 => string '68yfnckk3c5.png' (length=15)
      'text' => string 'text' (length=4)
      'type' => string 'article' (length=7)

I'm then looping over and accessing like so

{%- if feed is not empty -%}
    {% for feedItems in feed %}

      <!-- article -->
      {% if feedItems.type == 'article' %}
            <!-- image -->
            <div class="gridTile-article-image">
              {% for image in feedItems.images %}
                {{ image }} <br />
              {% endfor %}
            </div>

      {% endif %}


    {% endfor %}
  {% endif %}

This doesn't throw an error but also doesn't output anything, anyone got any ideas have to achieve this?

Thanks!

It looks like you're trying to output an entire array instead of one of its indices. feedItem.images is an array, as seen here:

 1 => 
array (size=10)
  'id' => int 17
  'images' => // The value of our index...
    array (size=3) // ..is an array
      0 => string 'xjv5y4asev2.png' (length=15)
      1 => string 'kwwss8f6r34.gif' (length=15)
      2 => string '68yfnckk3c5.png' (length=15)
  'text' => string 'text' (length=4)
  'type' => string 'article' (length=7)

My guess is that you have to refer to an index of images in the innermost block. For that, as this answer suggests, use the attribute function:
Accessing array values using array key from Twig

So your code would like like this:

{%- if feed is not empty -%}
{% for feedItems in feed %}

  <!-- article -->
  {% if feedItems.type == 'article' %}
        <!-- image -->
        <div class="gridTile-article-image">
          {% for image in feedItems.images %}
            {{ attribute(image, 0) }} <br /> // Assuming you want to print "xjv5y4asev2.png"
          {% endfor %}
        </div>

  {% endif %}


{% endfor %}
{% endif %}  

So I worked out what I was missing. The following (original) syntax I had worked fine but I made a very stupid human error.

<div class="gridTile-article-image">
  {% for image in feedItems.images %}
    {{image}}
  {% endfor %}
</div>

The reason this was returning blank was because the images array in the feed that I was querying was actually empty! I was looking at an array that was already populated but this wasn't of the type 'article' and therefore my code within that block wasn't returning any images because there weren't any!

Thanks for all your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM