3

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!

3
  • is it working without the inner loop? Commented Mar 27, 2015 at 11:46
  • 3
    No need to check for emptyness of feed. With Twig's for loop, you can do {% else %} No feed {% endfor %}. Commented Mar 27, 2015 at 11:47
  • Yep works without the inner loop just fine so I can return whats within the array, i'm just having trouble accessing an array within that array. Ah thanks D4V1D! Commented Mar 27, 2015 at 11:56

2 Answers 2

4

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 %}  
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Kaloyan, I tried this but it was throwing the error "Impossible to access an attribute ("0") on a string variable ("ws1c8e2y2n1.png")" I worked out my problem, see below, thanks for your help though!
Glad you sorted that out! Also, yes, when dealing with somebody else's API / feed (sounds like your situation), always do all the checks you can think of, no matter how stupid they may sound :)
2

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.