Tom Muck's Blog
News and Views
1 post
on 10/05/2005
Custom tag for RSS feed in ColdFusion
Wednesday, October 05, 2005 8:21:24 PM
I wrote a simple ColdFusion custom tag last night that you might find useful. The tag will generate an RSS feed. You pass a recordset/query to the tag and it will spit out an RSS feed. It is based partly on the RSS feeds I built for this site and Community MX, and also based on the custom tags I built for generating dynamic tables and csv files. Like the others, it is a rudimentary tag that you can build on.
Use the custom tag like this:
1. Put it in a directory where custom tags are stored. I usually just create a folder called tags and put all my tags inside. Save it as rss.cfm.
2. On the page that will become your RSS fed, add an import statement to the top of the file:
<cfimport prefix="mytag" taglib="tags">
3. Create your query:
<cfquery name="blah" datasource="#mydsn#">
SELECT NewsID, NewsDate, NewsTitle, NewsItem FROM MyTable
</cfquery>
4. Put the tag on the page, passing in the results of the query:
<mytag:rss rs=#blah#>
If you browse the page, the RSS should be visible.
There are several optional attributes, which are outlined below:
Attributes:
rs: Recordset/query to use for the RSS file (default: new query)
title: title tag in channel (default: RSS)
link: link tag in channel (default: http://#cgi.SERVER_NAME#)
description: description tag in channel (default: RSS feed for #attributes.link#)
webmaster: webmaster tag in channel (default: nobody@notavaliddomain.com)
items: Struct of item field names (default: structnew())
items.title: Field name for title of RSS item (default: title)
items.description: Field name for descriptionof RSS item (default: description)
items.link: Field name for link of RSS item (default: link)
items.pubdate: Field name for pubdate of RSS item (default: pubdate)
items.permalink: http address for link with ID from database (default: http://#cgi.SERVER_NAME#/?id=)
items.id: Primary key for picking record
striphtml: strip the html tags from the item (true/false, default: false)
wordcount: truncate item to number of words (default: empty string for no truncation)
maxrows: show x number of rows (default: empty string for all records)
debug: true/false value for debugging the feed (true turns of text/xml content type)
xslt: name of an XSL transformation file (default: empty string for no transformations)
If the query has the proper field names for an rss file (title, description, pubdate, link, and id for the primary key) you don't need to pass in field names. If your blog or news query uses completely different fieldnames, simply set them up in a struct before calling the tag, like this:
<cfsilent>
<!--- Create the query --->
<cfquery name="rsNews" datasource="#request.dsn#">
SELECT NewsID, NewsDate, NewsTitle, NewsItem FROM Blog
ORDER BY NewsDate DESC
</cfquery>
<!--- Set up the field name mapping --->
<cfset fieldmappings = StructNew()>
<cfset fieldmappings.pubdate = "NewsDate">
<cfset fieldmappings.id = "NewsID">
<cfset fieldmappings.title = "NewsTitle">
<cfset fieldmappings.description = "NewsItem">
</cfsilent>
<!--- Call the cf_rss tag --->
<mytag:rss rs=#rsNews# items=#fieldmappings#>
Alternatively, you can avoid the import statement and use old-style cf_ syntax:
<cf_rss rs=#blah#>
You can also pass in the name of an XSL transformation file. A sample file is included in the zip:
<mytag:rss rs=#rsNews# items=#fieldmappings# xslt="rss.xslt" >
I hope you find it useful. See it in action here. View the source here or download it here.
Category tags: Dreamweaver, ColdFusion
Posted by Tom Muck
Add comment |
View comments (0) |
Permalink
|
Trackbacks (0)
|
Digg This
1 post
on 10/05/2005
Before posting comments or trackbacks, please read the posting policy.