Hack 87 Syndicate Content with Movable Type

   

figs/moderate.gif figs/hack87.gif

Movable Type (MT) is a very flexible personal publishing system that is extremely popular in the weblogging world. One of its most powerful features is its ability to output content into any markup form you may need for easy syndication of content.

While the possible uses for Movable Type's (http://www.movabletype.org/) template engine are vast, for this hack we'll focus on the syndication formats that MT supports by default and some of the many things you can do with them. MT ships with templates for RSS 1.0 and 2.0 in addition to the current version of the emerging Atom format, Version 0.3. The RSS templates produce a syndication feed containing the last 15 posts for the weblog including the post title, link, primary category, author, issue date, and an excerpt. (MT has an excerpt field for each post. If that field was not populated it will automatically generate one from the body text. The length depends on the parameter set in the weblog configuration.) The Atom format provides the same information, but it also includes the full content of the post amongst other refined pieces of metadata. Let's walk through some common extensions and specialized syndication feeds.

6.9.1 Syndicating the Whole Post

Let's begin by adding the full text of the post to the RSS templates. In order to include both an excerpt and the full text, we need to use the content module so we can use its encoded tag. To do so, we would add this namespace declaration:

xmlns:content="http://purl.org/rss/1.0/modules/content/"

In the RSS 1.0 template you'll end up with:

<rdf:RDF  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"  xmlns:dc="http://purl.org/dc/elements/1.1/"  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"  xmlns:admin="http://webns.net/mvcb/"  xmlns:cc="http://web.resource.org/cc/"  xmlns:content="http://purl.org/rss/1.0/modules/content/"  xmlns="http://purl.org/rss/1.0/">

In the RSS 2.0 template you'll end up with:

<rss version="2.0"  xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/" >

With the content module namespace declaration in place, we can now insert some MT template tags to insert the full text into the RSS feeds. Go down toward the end of the templates and add this line between the item tags:

<content:encoded><$MTEntryBody encode_xml="1"$></content:encoded>

Save the template and then rebuild it. Your RSS feeds now will include the full text of your entries.

MT provides two body fields that some people like to use. If you are one of those people you would add this line instead:

<content:encoded><$MTEntryBody encode_xml="1"$><$MTEntryMore encode_xml="1"$> </content:encoded>

This inserts the content of both fields into the feed.

6.9.2 Including Trackback Links

Trackback pings are a means of remote hyperlinking. They were first introduced in MT and have been adopted by many other weblog tools since. Here is how we could include the Trackback ping URLs for each item and the Trackback pings that were made from them.

We begin by declaring the Trackback syndication module namespace in our root element.

xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"

Next we go down into the item tag and insert this markup:

<MTEntryIfAllowPings>   <trackback:ping rdf:resource="<MTEntryTrackbackLink encode_xml="1">"/>   <MTPings>     <trackback:about rdf:resource="<$MTPingURL encode_xml="1"$>" />   </MTPings> </MTEntryIfAllowPings>

By wrapping this block in the MTEntryIfAllowPings tags, only items that allow Trackback pings will have these tags inserted.

6.9.3 Creating Specialized Feeds

We can continue adding MT content to our feed by using the many syndication modules in existence. For an example of a very meta rich feed, see Ben Hammersley's "Making Feature-Rich, Movable Type RSS Files" (http://www.oreillynet.com/pub/a/javascript/2003/02/28/rss.html).

Let's go a different direction and focus on creating different specialized feeds. Rather then include both an excerpt and the full text in the same feed, you may opt to offer separate feeds. Start with one of the default templates, copy the template text, and create a new template. Paste the template text into the new record and follow the previous steps, but replace the item description element with content:encoded. (Be sure to use a different filename for the output.)

Even with the description available, it is a good practice to use content:encoded because some versions of RSS define description as a plain-text excerpt with a limit of 500 characters. Any capable aggregator will be able to handle this tag and have a chance to handle the full text differently.

If you are providing a full text feed, you may want to consider cutting down on the number of entries you include in the feed to conserve bandwidth. To do so is just a matter of adjusting the lastn attribute of the MTEntries element. For example, to reduce the number of items to the last three, change the MTEntries element to:

<MTEntries lastn="3">

Now your full text RSS feeds would include only 3 items, substantially reducing its size and saving bandwidth each time an aggregator retrieves the file.

6.9.4 Category Feeds

Assuming you assign categories to your posts, MT can be used to create separate syndication feeds for the latest entries in each.

To begin, copy one of the default syndication templates to your clipboard. Setting up per-category feeds doesn't require making any changes to the template, but you may make changes anyway depending on preference and effect. Go to the Templates listing by clicking the Templates button in the left-hand navigation bar. Scroll down and click the "Create new archive template" link. Give your template a title and paste the template code from your clipboard into the Template Body field. Click Save.

Archive templates need to be associated with an archive type. To make this assignment, click Weblog Config in the left-hand toolbar and then click Archiving from the links at the top of the page.

Make sure Category archives are checked so they are activated. If they are not, check them at this time and press Save.

Click the "Add new" button, and a pop-up will appear with two drop-down boxes. Select the Category archive type the name of the Category syndication template you created, and then click the Add button.

That's it. The pop-up will close and the archive configuration page will refresh to list the new template as a Category archive template. At this time you can define the filenaming scheme if you like. See the MT documentation for more information.

6.9.5 Syndicating Comments

If you have an active comments board on your Movable Type weblog, you can provide readers with a syndication feed of the most recent comments, which they can track with their aggregator.

Begin by creating a new index template and pasting the default RSS template into the template body. Give it a unique and descriptive name and press Save.

You will probably want to add the content namespace declaration and modify the channel information to reflect the nature of this syndication feed. These modifications are trivial, so we'll skip over them and get to the heart of the matter in the item tags.

We replace the MTEntries element and its contents with the following template code:

<MTComments lastn="20"> <item>         <title><$MTCommentAuthor$>: <MTCommentEntry><$MTEntryTitle$>            </MTCommentEntry> (<$MTCommentDate             format="%Y-%m-%dT%H:%M:%S"$><$MTBlogTimezone$>)</title>         <content:encoded><$MTCommentBody             encode_xml="1"$></content:encoded>         <link><$MTCommentEntryLink$>#<$MTCommentID             zero_pad="5"$></link>         <dc:contributor><$MTCommentAuthor$></dc:contributor>         <dc:date><$MTCommentDate             format="%Y-%m-%dT%H:%M:%S"$><$MTBlogTimezone$></dc:date> </item> </MTComments>

This will insert the last 20 comments made in the weblog. Since comments don't have titles, we create our own with various bits that make it easily scannable and unique.

If you have decided to use RSS 1.0, you will have to modify the items element block also.

<items>     <rdf:Seq><MTComments lastn="20">       <rdf:li rdf:resource="<$MTCommentEntryLink$>#<$MTCommentID           zero_pad="5"$>" />     </MTComments></rdf:Seq> </items>

Click the Save button and then Rebuild. Presto! Your readers can now track the comments with their aggregators.

As you can see, MT has a very flexible and powerful means of creating markup, so there are a lot of other options we could have explored. You'll have to explore its potential on your own.

If you are looking to insert syndication content into your weblog layout, look to the mt-feed plug-in (formerly mt-rssfeed).


Timothy Appnel



XML Hacks
XML Hacks: 100 Industrial-Strength Tips and Tools
ISBN: 0596007116
EAN: 2147483647
Year: 2006
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net