The Aardvark Speaks : essence, effervescence, obscurity. Established 2002. A weblog by Horst Prillinger. ISSN 1726-5320


September 02, 2003

RSS template tutorial, part 2

This is a highly technical post in response to an email from a reader who wanted to know how to get comment data, comment text, trackback data and incoming trackback pings into one's RSS feed in Movable Type.

I'm not really an expert in RSS, so I might talk nonsense here (corrections welcome), but the feeds that I cobbled together here surprisingly do validate, so I can't be all that wrong.

All of this is done according to Dave Winer's "unfunky" RSS 2.0 specs; please bear in mind that this may not work with other RSS dialects (or RSS 1.0, which is an entirely different language). One reason why I like Dave's specs is that they are almost self-explanatory, and it's fairly easy to write your own feed templates for Movable Type once you get a hang of MT's template tags. The RSS 2.0 specs can be found at http://backend.userland.com/rss. Everything I'm writing here is pieced together from there, and from the Movable Type online help.

Comment data

Adding comment data to your RSS feed is simple. All you need to do is add this code to your <item> tag:

<item>
...
<MTEntryIfAllowComments>
<comments><$MTCGIPath$><$MTCommentScript$>?entry_id=<$MTEntryID$></comments>
</MTEntryIfAllowComments>

...
</item>

This checks if comments are allowed and then adds the URL of the comments page into the <comments> tag. That's it.

Trackback data

Adding trackback data is slightly more complicated, as it isn't part of the original RSS 2.0 specs. There is, however, a so-called namespace for a trackback model that can be implemented. To do so, you need to specify that you are using it in the RSS delimiter at the beginning of the RSS file:

<rss version="2.0" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">

You then have to follow the specifications of the Trackback module as specified at this address. Two RSS items are provided, <trackback:ping> containing the pingable Trackback URL for this entry, and <trackback:about> containing the outgoing pings sent with this entry. In your Movable Type template, the respective code is put inside <item> and looks like this:

<item>
...
<MTEntryIfAllowPings>
<trackback:ping><$MTEntryTrackbackLink$></trackback:ping>
</MTEntryIfAllowPings>
<MTPingsSent>
<trackback:about><$MTPingsSentURL encode_xml="1"$></trackback:about>
</MTPingsSent>

...
</item>

This causes <trackback:ping> to be generated once and <trackback:about> once for every ping sent.

Comments text and incoming Trackback pings - a caveat

Even though I will be explaining how to incorporate comments and incoming pings into your RSS feed, I'd like to point out that this is not recommended; the reason is simply that with every new comment or incoming ping, the feed will be updated, and, depending on their newsreader, subscribers may receive the same news item over and over again with each new update, which, again depending on the newsreader, can be a major annoyance.

If you think you must include these things in your feed, here is how it goes:

Comments text

The comments are part of the item's text body; as such, they go into the entry's <description>, along with the rest of the text. If you want to format the text so that it becomes readable (it isn't if you don't), you need to encode it as CDATA. If you haven't used this before, take care to use the correct number of brackets. Here is what you need to add to the <description> of the item:

<item>
...
<description>
...
<MTEntryIfAllowComments>
<MTComments sort_order="ascend"> <![CDATA[<p><$MTCommentAuthor$> said on <$MTCommentDate format="%x, %X"$>:<br />
<$MTCommentBody convert_breaks="0"$></p>]]>
</MTComments>
</MTEntryIfAllowComments>
...
</description>
...
</item>

First, <MTEntryIfAllowComments> checks if comments are turned on for this post; if yes, <MTComments sort_order="ascend"> lists them in ascending order (first one first; last one first is also possible if you use "descending"). You then get the name of the comments author and the date, plus the text of the comment for every comment that is associated with the post (the convert_breaks attribute is needed to get rid of extra HTML which may cause problems).

If you just want to add a note how many comments have been posted without the actual comments text, this is of course also possible using the corresponding MT template tags:

<item>
...
<description>
...
<MTEntryIfAllowComments>
<![CDATA[<p><$MTEntryCommentCount$> people have commented on this so far.</p>]]>
</MTEntryIfAllowComments>
...
</description>
...
</item>

Incoming Trackback pings

Incoming Trackback pings work pretty much the same way. Like comments, they should go into the item's <description>. Again, we need CDATA as we are using HTML tags:

<item>
...
<description>
...
<MTEntryIfAllowPings>
<MTPings><![CDATA[<p>This article received a trackback ping from:<br />
<$MTPingURL$></p>]]></MTPings>
</MTEntryIfAllowPings>
...
</description>
...
</item>

As you would expect, a mere count of Trackback pings works just like the comments count:

<item>
...
<description>
...
<MTEntryIfAllowPings>
<![CDATA[<p><$MTEntryTrackbackCount$> TrackBack pings.</p>]]>
</MTEntryIfAllowPings>
...
</description>
...
</item>

The other extreme would be to include the full monty:

<item>
...
<description>
...
<MTEntryIfAllowPings>
<MTPings>
<![CDATA[<p>This article received a trackback ping from:<br />
<a href="<$MTPingURL$>"><$MTPingTitle$></a><br />
Excerpt: <$MTPingExcerpt$><br />
Weblog: <$MTPingBlogName$><br />
Tracked: <$MTPingDate$></p>]]>
</MTPings>
</MTEntryIfAllowPings>
...
</description>
...
</item>

Please be aware that, as I said above, not only will this blow up your feed to huge proportions, if somebody is subscribed to your feed, he may receive the entry over and over again every time somebody adds a new Trackback ping.

Example feeds

So anyway, to wrap it up here's a recommended feed that includes the harmless data for comments and pings, based on my template #1 from last week. It does not include comments or trackback text, just the links to the comments and trackback pages. The things I outlined in this posting are indicated in bold type:

<?xml version="1.0" encoding="<$MTPublishCharset$>"?>
<rss version="2.0" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">

<channel>
<title><$MTBlogName remove_html="1" encode_xml="1"$></title>
<link><$MTBlogURL$></link>
<description><$MTBlogDescription remove_html="1" encode_xml="1"$></description>
<language>en</language>
<copyright>Copyright <MTEntries lastn="1"><$MTEntryDate format="%Y"$> <$MTEntryAuthor$></MTEntries></copyright>
<lastBuildDate><MTEntries lastn="1"><$MTEntryDate format="%a, %d %b %Y %H:%M:%S"$> GMT</MTEntries></lastBuildDate>
<docs>http://backend.userland.com/rss</docs>
<generator>Movable Type <$MTVersion$></generator>
<managingEditor><MTEntries lastn="1"><$MTEntryAuthorEmail spam_protect="1"$> (<$MTEntryAuthor$>)</MTEntries></managingEditor>
<webMaster><MTEntries lastn="1"><$MTEntryAuthorEmail spam_protect="1"$> (<$MTEntryAuthor$>)</MTEntries></webMaster>

<MTEntries lastn="15">
<item>
<title><$MTEntryTitle remove_html="1" encode_xml="1"$></title>
<link><$MTEntryLink encode_xml="1"$></link>
<description><$MTEntryBody encode_xml="1"$><MTEntryIfExtended><$MTEntryMore encode_xml="1"$></MTEntryIfExtended></description>
<category><$MTEntryCategory remove_html="1" encode_xml="1"$></category>
<MTEntryIfAllowComments>
<comments><$MTCGIPath$><$MTCommentScript$>?entry_id=<$MTEntryID$></comments>
</MTEntryIfAllowComments>
<MTEntryIfAllowPings>
<trackback:ping><$MTEntryTrackbackLink$></trackback:ping>
</MTEntryIfAllowPings>
<MTPingsSent>
<trackback:about><$MTPingsSentURL encode_xml="1"$></trackback:about>
</MTPingsSent>
<guid isPermaLink="true"><$MTEntryPermalink$></guid>
<pubDate><$MTEntryDate format="%a, %d %b %Y %H:%M:%S"$> GMT</pubDate>
</item>
</MTEntries>

</channel>
</rss>

And this is the killer feed that includes all comments and trackbacks in full text (not recommended). Again, the things explained in this article are highlighted in bold type:

<?xml version="1.0" encoding="<$MTPublishCharset$>"?>
<rss version="2.0" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">

<channel>
<title><$MTBlogName remove_html="1" encode_xml="1"$></title>
<link><$MTBlogURL$></link>
<description><$MTBlogDescription remove_html="1" encode_xml="1"$></description>
<language>en</language>
<copyright>Copyright <MTEntries lastn="1"><$MTEntryDate format="%Y"$> <$MTEntryAuthor$></MTEntries></copyright>
<lastBuildDate><MTEntries lastn="1"><$MTEntryDate format="%a, %d %b %Y %H:%M:%S"$> GMT</MTEntries></lastBuildDate>
<docs>http://backend.userland.com/rss</docs>
<generator>Movable Type <$MTVersion$></generator>
<managingEditor><MTEntries lastn="1"><$MTEntryAuthorEmail spam_protect="1"$> (<$MTEntryAuthor$>)</MTEntries></managingEditor>
<webMaster><MTEntries lastn="1"><$MTEntryAuthorEmail spam_protect="1"$> (<$MTEntryAuthor$>)</MTEntries></webMaster>

<MTEntries lastn="15">
<item>
<title><$MTEntryTitle remove_html="1" encode_xml="1"$></title>
<link><$MTEntryLink encode_xml="1"$></link>
<description>
<$MTEntryBody encode_xml="1"$>
<MTEntryIfExtended><$MTEntryMore encode_xml="1"$></MTEntryIfExtended>
<MTEntryIfAllowComments>
<![CDATA[<p><b><$MTEntryCommentCount$> comments, listed in ascending order (if any)</b></p>]]>
<MTComments sort_order="ascend">
<![CDATA[<p><$MTCommentAuthor$> said on <$MTCommentDate format="%x, %X"$>:<br />
<$MTCommentBody convert_breaks="0"$></p>]]>
</MTComments>
</MTEntryIfAllowComments>
<MTEntryIfAllowPings>
<![CDATA[<p><b><$MTEntryTrackbackCount$> Trackback pings</b></p>]]>
<MTPings>
<![CDATA[<p>This article received a trackback ping from:<br />
<a href="<$MTPingURL$>"><$MTPingTitle$></a><br />
Excerpt: <$MTPingExcerpt$><br />
Weblog: <$MTPingBlogName$><br />
Tracked: <$MTPingDate$></p>]]>
</MTPings>
</MTEntryIfAllowPings>
</description>

<category><$MTEntryCategory remove_html="1" encode_xml="1"$></category>
<MTEntryIfAllowComments>
<comments><$MTCGIPath$><$MTCommentScript$>?entry_id=<$MTEntryID$></comments>
</MTEntryIfAllowComments>
<MTEntryIfAllowPings>
<trackback:ping><$MTEntryTrackbackLink$></trackback:ping>
<MTPingsSent>
<trackback:about><$MTPingsSentURL encode_xml="1"$></trackback:about>
</MTPingsSent>
</MTEntryIfAllowPings>
<guid isPermaLink="true"><$MTEntryPermalink$></guid>
<pubDate><$MTEntryDate format="%a, %d %b %Y %H:%M:%S"$> GMT</pubDate>
</item>
</MTEntries>

</channel>
</rss>

As I said, all of this is "unfunky" RSS 2.0 and it validated when I last checked it. The code does not work as part of an RSS 1.0 feed, which uses an entirely different model.

Posted by Horst on September 2, 2003 01:00 PM to metablogging | Tell-a-friend
Comments
Thomas N. Burg said on September 3, 2003 12:19 PM:

Hi Horst,
I'm thinking of playing with MT. Thus your post is very helpful. I've got one question. Can I restrict the comments-feed to just create that there is a comment posted or just the first word or so. I'm going to need that for privacy reasons. Do you have any idea?

Thanks Thomas

Horst said on September 3, 2003 12:42 PM:

No problem at all. I have amended the text above (see under "Comments text") with an example.

Comments have been closed for this entry.


© Copyright 2002-2008 Horst Prillinger, 

Most of the stuff on this page is fiction. Everything else is my private opinion. Please read the disclaimer.

Valid XHTML 1.0! Powered by Movable Type Made with a Mac