2

Probably pure twig:

I am building an array from nodes taken from a RSS-feed where one of the keys contains a colon which of course breaks everything. Like so:

Node in feed:

<media:thumbnail url="stuff I like to get." />

My twig:

{% for node in feed %}
        {% set latestArticles = latestArticles|merge([{
                'timestamp': node.pubDate|atom,
                'title': node.title,
                'uri': node.link,
                'description': node.description,
                'thumbnail' : node.media:thumbnail,
            }]) %}
        {% endif %}
    {% endfor %}

So how do I get media:thumbnail?

I have tried different things but without luck.

6
  • Have you tried brackets notation, i.e. node['media:thumbnail']? Sep 10 '18 at 5:17
  • Yes, I did. It says: Key "media:thumbnail" for array with keys "title, link, pubDate, category, guid, description, dc:creator" does not exist. Sep 10 '18 at 5:44
  • 1
    That suggests that the structure of the node object isn’t what you think it is. Try enabling devMode and do {{ dump(node) }} to see the object’s contents and structure Sep 10 '18 at 16:48
  • 1
    Haven’t thought about dump. No idea, why. You’re right, it’s an array. So the correct code is: 'img' : node['media:content'][1]['@url'] Thanks! Sep 10 '18 at 17:14
  • @MarcusScheller would you mind adding that as an official answer in case it ends up helping someone in the future?
    – Brad Bell
    Sep 10 '18 at 23:32
1

As @mats-mikkel-rummelhoff suggested, that data type is an array. So I was able to get the desired value by using:

'img' : node['media:content'][1]['@url']

Thanks, Mats.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.