Using in your Templates

While you can create a feed task to insert data as entries, there are times which you may prefer to capture feed data on-demand, rather than saving as an entry. You can easily do this through your twig templates using the below.

Feeds are cached for performance (default to 60 seconds), which can be set by a tag parameter, or in the plugin settings.

{% set params = {
    url: 'http://path.to/feed/',
    type: 'xml',
    element: 'item',
    cache: 60,
} %}

{% set feed = craft.feedme.feed(params) %}

{% for node in feed %}
    {# Your template code goes here #}
{% endfor %}

Parameters

  • url (string, required) - URL to the feed.
  • type (string, optional) - The type of feed you're fetching data from. Valid options are json or xml (defaults to xml).
  • element (string, optional) - Element to start feed from. Useful for deep feeds.
  • cache (bool or number, optional) - Whether or not to cache the request. If true, will use the default as set in the plugin settings, or if a number, will use that as its duration. Setting to false will disable cache completely.

Example template code #

<?xml version="1.0" encoding="UTF-8" ?>
<entries>
    <entry>
        <title>Monday</title>
        <item>
            <title format="html">Event 1</title>
            <type>All-day</type>
        </item>
    </entry>
    
    <entry>
        <title>Tuesday</title>
        <item>
            <title format="html">Event 2</title>
            <type>Half-day</type>
        </item>
    </entry>
</entries>

With the above example XML, we would use the following Twig code to loop through each entry to extract its data.

{% set params = {
    url: 'http://path.to/feed/',
    type: 'xml',
    element: 'entry',
    cache: 60,
} %}

{% set feed = craft.feedme.feed(params) %}

{% for node in feed %}
    Title: {{ node.title }}
    Item: {{ node.item.title['@'] }}
    Item Format: {{ node.item.title['@format'] }}
    Type: {{ node.item.type }}
{% endfor %}

{# Producing the following output #}
Title: Monday
Item: Event 1
Item Format: html
Type: All-day

Title: Tuesday
Item: Event 2
Item Format: html
Type: Half-day

There's a special case for XML-based feeds, which is illustrated above when attributes are present on a node. To retrieve the node value, use ['@'], and to fetch the attribute value, use ['@attribute_name'].

Get started with Feed Me

Available for Craft 2, with Free and Pro versions.