Mika Friman
asked this on Jan-04 13:22
Hey,
How to easily create RSS-feed from the contents of Data-class. E.g. I have got news listed in web pages, but would like to have RSS-feed from those to be shared for possible readers?
And another question, how to include RSS-feed from other pages to page "News from our partners" for example, taking first item from the whole feed found from partner's pages.
-Mika friman
To answer your second question we are going to add an element that reads xml and parse the data to make an object. We will notice you when it's done.
Building RSS feed from the data table is easy. There are several different versions of RSS and you can use the one that is described below or the one that you prefer.
Start with making a new component that doesn't use HTML skeleton. Add plain text element and type the start of the RSS feed
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
After that comes the channel information which we will add to the same plain text element. If you want the text inside tags to come from variables you could use hammerscript or print element to deal that.
<channel>
<title>RSS Title</title>
<description>This is an example of an RSS feed</description>
<link>RSS feed URL</link>
<lastBuildDate>Mon, 06 Sep 2010 00:01:00 +0000</lastBuildDate>
<pubDate>Mon, 06 Sep 2009 16:45:00 +0000</pubDate>
Now we have to add item stuff. Those will be the actual news items and they will come from the data table. We start by adding list initializer and list iterator to the component to list data that we want to show. Inside list iterator we add plain text element where we type item information. Use hammerscript or print element to add text from variables inside tags.
<title>{$var_private.obj.title}</title>
<description>{$var_private.obj.content}</description>
<link>URL to this item</link>
<guid>unique string per item</guid>
<pubDate>{$var_private.obj.pubDate}</pubDate>
</item>
And finally we add closing tags for the channel and rss by adding once again plain text element after list iterator.
</channel>
</rss>
That's it! Save the component and we are done. Preview to see how does your RSS feed look like and remember to validate it with RSS validator.
The final code should look like this.
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Title</title>
<description>This is an example of an RSS feed</description>
<link>RSS feed URL</link>
<lastBuildDate>Mon, 06 Sep 2010 00:01:00 +0000</lastBuildDate>
<pubDate>Mon, 06 Sep 2009 16:45:00 +0000</pubDate>
<item>
<title>Example entry</title>
<description>Here is some text containing an interesting description of the thing to be described.</description>
<link>URL to this item</link>
<guid>unique string per item</guid>
<pubDate>Mon, 06 Sep 2009 16:45:00 +0000</pubDate>
</item>
</channel>
</rss>