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)