LuzCannon.com

The Work of David Brooks

External Reading Upgrade

February 14, 2008

I’ve always been a fan of displaying links to external content on my own website. I take note of all the times I found something useful or interesting by looking at what other people are referencing. For the longest time I had a stationary unordered list that displayed the last five or ten things I had hand written into place but I secretly envied people who could even ship that content out via RSS. With Textpattern that was a tiny bit problematic as some of you may know, but with a little ingenuity and modifications there is a way to do exactly that, from the safety of your own Textpattern install.

Now, some people have probably solved the problem in alternate ways, and that’s fine. This probably isn’t the only way to accomplish the task at hand, it’s “a” way.

I’d like to start with giving credit to Michael Gravel who wrote an article and a plugin for Podcasting with Textpattern. I had used the concept he wrote about for a client’s site with great success. It was just as easy to incorporate as I had hoped. Not that there’s anything wrong with the article he wrote but when I got the idea to finally include an external resources feed I had to do a lot of tweaking with the templates themselves because of the differences between the Podcasting and article RSS schemes. So, I modified his concept as follows:

The Basics

The concept Michael writes about goes like this, you take your RSS feed template use Textpattern’s built in tags to populate the content as necessary. To make that work very efficiently you add a form that will pull the necessary data from your posted articles. That’s the entirety of the concept and it works for both Podcasting or plain RSS feeds.

RSS Template

First, I created a page in Textpattern called “external readings” which in my case will only serve as a mechanism for subscription. In Michael’s example he had created two sections, one for posting and one for subscribing. In my case I only wanted the subscription piece because I’m going to use Textpattern’s custom article tag to pull the content of that “section” onto my front page. More on all of that later.

I took a look at Textpattern’s default RSS feeds and replicated the formatting into the page template. After stripping out some of the excess XML it looks like this. (My content is still in here as a working example, you’ll need to pull that out and change it to your values.)

 <txp:mg_setheader /><?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:content="http://purl.org/rss/1.0/modules/content/" 
xmlns:atom="http://www.w3.org/2005/Atom">
<channel><generator>http://textpattern.com/?v=4.0.4</generator>
<title>LuzCannon.com External Readings</title>
<link>http://luzcannon.com/external/</link>
<description>Things I find useful, funny or interesting</description>
<pubDate>Thu, 14 Feb 2008 08:00:00 EST</pubDate>
<txp:article_custom form="externalrss" limit="10" section="external" />
<atom:link href="http://www.luzcannon.com/external" 
rel="self" type="application/rss+xml" />
</channel> 
</rss>

In that chunk we’re writing the basic template and allowing for Textpattern to drop in a form that we’re going to create called “externalrss”. Once this stuff has been changed to represent the content on your website we’ll move on to the installation of the plugin and the custom category information.

Plugin and Custom Field Setup

Next you’ll need download Michael’s plugin. When you install it and enable it from the Admin menu it will change the heading type of the document from which you call it. You’ll see it listed in the example above, it’s the first line of text. If you don’t list it on the same line as the XML version it will insert a blank space and register data before the XML version. That in result will often cause the feed to “freak out”, that’s the technical term. If you don’t list it at all you’ll get a lovely error pointing out that your content is being delivered as HTML and not XML.

Next, from the admin menu we need to go to “preferences” and then “advanced.” Toward the bottom you’ll see the list of “custom fields.” I added one to my list called “external.” This will be the field that I use to give the “article” a link to the original external content. You can call it whatever makes sense to you as long as you’re consistent in the following form.

Form Creation

In order for our content to be placed into the page template in the correct way we need to create a form that will format it appropriately. So, go on over to the “forms” page, which is found under “presentation.” From there you can create an article form. If you’re using the above code in its entirety you’ll need to call this one “externalrss”. This is what the form looks like on my site.

<item><title><txp:title /></title>
<description>
<![CDATA[
<txp:body />
]]>
</description>
<link><txp:custom_field name="external" /></link>
<pubDate><txp:posted format="%a, %d %b %Y %I:%m:%S" /> EST</pubDate>
<dc:creator>David Brooks</dc:creator>
<guid><txp:custom_field name="external" /></guid>
</item>

As with the form from earlier you’ll need to replace my content with your content. Mostly that will just be the name of the creator in this case, however, it could be that you want extra functionality or have changed the custom fields, etc.

Section Creation

The last step in the creation of the feed is the publication section. You’ll need to create a new section (presentation – sections) called “external.” You could really call it whatever you want, this part is up to you and depends on how you want it to be referenced.

All you have to do is fill in the name fields and tell it to use the “external reading” page that we created previously. Under that you will most likely want to select “no” for all those radio buttons since most of this will not be applicable to this type of feed.

With any luck this should result in a functional RSS feed. In my case it’s listed as www.LuzCannon.com/external but it will show up on your site by whatever you named the section.

Custom Article List

As I stated before, the point of creating this was to increase the functionality of the feed and allow me to stop creating the unordered list by hand. To do that I’m simply going to add in one more form and make a reference to it.

This form will look something like this, obviously you might want to format the output differently for your particular context.

<li><a href="<txp:custom_field name="external" />"><txp:title /></a></li>

This will pull in the custom field that we created before and place that inside the href of our link. It will then use the default title tag to place the title of the external article.

To call in that article from somewhere in the site you could write:

<ol>
<txp:article_custom form="externalList" section="external" limit="5" />
</ol>

Wherever you put that chunk you should see your list of external articles.

Alternate Ideas

Michael’s original concept was written for podcasting, this article is intended for external RSS feeds but ultimately the concept could be used for anything. Once you’ve created the template it could be easily adapted to make alternate custom feeds, like a sectional RSS feed. The possibilities are endless.

Tagged:

rss textpattern web