How to Add RSS Feeds in Email Templates

We can help you add RSS feeds in your MessageGears email templates for your email marketing team

Introduction

With MessageGears Accelerator, injecting data into your Templates is a snap. No custom integration logic is required for most sources. Whether you are using your internal Data-Warehouse or a simple URL, your Templates will access the data in the same way via the FreeMarker rendering engine. We will discuss Query Datasources in future blog posts, but adding a URL Datasource is a simple matter of providing the URL. There’s really nothing to it. As long as the URL points to a well formed XML document, then Accelerator will make the data available to your Template using simple variable names. A great example of a URL Datasource is an RSS Feed. Let’s walk through adding an RSS Feed as a Context Data source.

Getting Started

Let’s use the BBC’s RSS Feed for this example. Like most organizations, the BBC actually has several feeds. Here’s the one that I used: https://feeds.bbci.co.uk/news/rss.xml. It’s the feed for their top news stories. To add this feed to Accelerator, do the following:

  1. Navigate to the “List Management” section in Accelerator
  2. Click on “URL Datasources” in the sub-navigation under “List Management”
  3. Click the “Add URL Source” button
  4. Type “BBC RSS Feed” in the name field
  5. Select “Context” as the Data Type
  6. Paste “https://feeds.bbci.co.uk/news/rss.xml” in the URL field
  7. Click the “Save” button

Using the Datasource in a Template

You can use this Datasource in your Template just by generating sample Context Data from the “BBC RSS Feed”, then you can use the sample data in your any personalizable sections of your Template (usually the HTML or Text section.)

  1. Create a Template as normal
  2. Navigate to “Content Management” > “Templates” > [Your-Template-Name] > “Context Data”
  3. Select “BBC RSS Feed” from the far right dropdown and click the “Refresh” button
  4. Click the “Save” button (scroll to the bottom:)
  5. Navigate to “Content Management” > “Templates” > [Your-Template-Name] > “HTML”
  6. Paste in the following and click the “Save” button:
    <h1>${rss['/rss/channel/title']}</h1>
    <#list rss['/rss/channel/item'] as item>
        <h3><a href='${item.link}'>${item.title}</a></h3>
        <p>${item.description}</p>
    </#list>

Now you should be able to go to “Content Management” > “Templates” > [Your-Template-Name] > “Personalization” to see a rendered version, or send yourself a Test message.

Line by Line Explanation

This short snippet prints out each item in the feed along with a link to the original article. You’ll see two slightly different notations for referencing elements in the Datasource: XPath and FreeMarker variables. The XPath notation (${rss[‘/rss/channel/item[0]/title’]}) is powerful because it can handle Namespaces and skip unnecessary elements. The FreeMarker notation (${item.title}) is easier but less flexible. Here’s a quick line by line explanation of what happens at render time:

  1. prints out the title of the RSS Feed
  2. loops through each item in the feed and assigns it to the variable “item”
  3. prints out the title of an item in the feed and provides a link to the article
  4. prints out the description of the item in the feed
  5. closes the loop

A More Advanced Example

If you think that the above example is a piece of cake, then take a few moments to look at the example below. The main difference between the two is the use of XML namespaces and limiting the number of items to display.

    <#ftl ns_prefixes={'media':'https://search.yahoo.com/mrss/'}>
    <h1>${rss['/rss/channel/title']}</h1>
    <#list rss['/rss/channel/item'] as item>
      <#if item_index lt 5 >
        <h3><a href='${item.link}'>${item.title}</a></h3>
        <img src='${item['media:thumbnail'][1].@url}' />
        <p>${item.description}</p>
      </#if>
    </#list>

Conclusion

The key takeaway here is that the rendering engine will let you easily reference all of the data provided by your Datasource in a consistent and powerful manner. Whether you are using an RSS Feed, a generated SOAP response, or a hand hacked XML file, you will have easy access to the data. Good luck and send any questions to support@messagegears.com!