Using getResources to generate an RSS feed
Posted by garryn on Feb 5, 2010 | Filed under: MODx Revolution
A question came up on the MODx forums today about whether it is possible to use the getResources snippet to generate an RSS feed in MODx Revolution. Sure it is!
Set up the RSS resource
Create a document, use the blank template, set the content type to XML and add the following content:
<rss version="2.0">
<channel>
<title>[[*pagetitle]]</title>
<link>[[++site_url]]</link>
<description>[[*description]]</description>
<lastBuildDate>[[!getTime?format=`%a, %d %b %Y %H:%M:%S %z`]]</lastBuildDate>
<language>en-gb</language>
[[!getResources?
&parents=`3`
&limit=`10`
&sortby=`createdon`
&sortdir=`DESC`
&includeTVs=`1`
&includeContent=`1`
&tpl=`rssListing`
]]
</channel>
</rss>
Adjust the &parents value to the container where you're saving your pages that you wish to be in your RSS feed. You'll also notice I have a getTime snippet call in there for the lastBuildDate element, that's just a snippet that returns the current time in a particular format. The snippet content is:
return strftime($scriptProperties['format']);
Create a CDATA output filter
To avoid any potential problems with CDATA tags and the MODx parser, use a custom output filter to wrap the fields in CDATA tags. The snippet used for the output filter is extremely simple:
return "<![CDATA[{$input}]]>";
Add this snippet to your MODx install and call it toCDATA.
Add the rssListing chunk
Now all that remains is to add the chunk that will be used for each item within the RSS feed. Here are the contents:
<item>
<title>[[+pagetitle:toCDATA]]</title>
<link>[[~[[+id]]]]</link>
<guid>[[~[[+id]]]]</guid>
<pubDate>[[+createdon:strtotime:date=`%a, %d %b %Y %H:%M:%S %z`]]</pubDate>
<description>[[+introtext:toCDATA]]</description>
</item>
Preview your RSS feed
If you have followed all the steps in this post, then you should have a fully-functional RSS feed.
Kudos goes to Jason Coward for the tip on using the CDATA output filter (and for writing the getResources snippet)
Clayton:
Aug 23, 2010 at 02:38 PM
Works great! Thanks a bunch.
Martin:
Nov 16, 2010 at 03:23 AM
Hi. I'm following the instructions with Revo, but I'm not getting a result. I've set up the snippets and made sure the resource is published. I have getResources installed.
However, when I preview the document, I get nothing.
Chrome gives me:
JX3:
Mar 17, 2011 at 05:59 PM
I'm not quite sure that have I done everything correctly, but the link only outputs /resource-alias. I'm trying to get the feed to work with twitterfeed, but had no luck as the link is missing my domain. I don't have anywhere - should I?
I fixed this by modifying the Tpl to [[!++site_url]][[~[[+id]]]] and now it seems to work, but I presume it should work correctly without this modification?
paul:
Sep 13, 2011 at 02:02 AM
how about images, links?