Was this page helpful?

RSS feeds

    Please discuss this template in this forum thread.

    Table of Contents

    Introduction

    This is a very simple template to embed RSS feeds onto a page, an alternative to the feed extension that allows for more customization because you can go and hack the code to make it look any way you want.  It is presently designed for RSS 2.0 format that works fine with the feeds I've tried so far.  The code will likely need reworking to work with other formats.  This is basically a simple example of what can be done with a small bit of Dekiscript.

    The content show/hide functionality requires the CollapseItem template to be installed as well.  And of course, this template requires Lyons (9.02.x).

    Important Limitations

    This template does not implement a proper RSS/ATOM parser.  It uses simple xpath expressions to fetch the required data, currently optimized for the Wordpress feeds we use at my company.  It also works with the Mindtouch developer blog (used as an example below).  However, there are many RSS variations, and as a result it's very possible this won't parse your feeds correctly.

    I have little interest in incorporating a full feed parser here, but what I will do in the future support a couple of additional formats, and refactor the code so that it is easier to customize for your needs.  Subscribe to this page or the forum thread for notifications.

    History

    Version Date Author Description
    1.01 09/11/2009 neilw Fixed "creator" bug (thanks brentg!)
    1.0 07/31/2009 neilw First published version

    How to install it

    1. Create a template, call it "Template:FeedTable" (or rename as you desire, of course).  You must have "unsafecontent" permission for this to work.
    2. Paste the code block at the end of this page into the template into a block of style "Dekiscript".  Make sure there isn't an extra blank paragraph after the Dekiscript block (verify this every time you edit the template!!!!)  Save.
    3. If you haven't already done so, install CollapseItem.

    How it works

    The template takes three arguments:

    FeedTable(uri, entries, content)

    Where:

    Argument Type Default Description
    uri uri NA (required) URI of RSS2.0 feed
    entries num? 10 number of entries to display.  If "0", display all entries in the feed.
    content str? none If "summary", show the content summary in the table.  If "full", show the complete feed entry in a collapsible format (see examples.)  If omitted, just shows entry names.  Note that not all feeds include full content, so the "full" option may or may not work for you.

     

    The table is assigned class="table", which on the Fiesta skin looks nice, but on this wiki apparently has no styling associated with it.  So I've added this little stylesheet to this page to shade the header:

    <style type='text/css'> table.table th { background-color:rbg(224,224,224) } </style>

    Examples

    Here's the Mindtouch Dev blog, using various options.

    First, all defaults: no content, 10 items.  Note that in this mode I chose not to boldface the article titles, because I thought it looked better this way:

    FeedTable('http://blog.developer.mindtouch.com/feed/')
    missing value for parameter 'uri' (index 0) (click for details)

    Now show 5 items with summaries, and bold article titles:

    FeedTable('http://blog.developer.mindtouch.com/feed/', 5, 'summary')
    missing value for parameter 'uri' (index 0) (click for details)

    Finally, 10 items with full collapsible content:

    FeedTable('http://blog.developer.mindtouch.com/feed/', 10, 'full')
    missing value for parameter 'uri' (index 0) (click for details)

    Template code

    //
    // FeedTable
    // by neilw, 2009
    //
    // Version history:
    //    1.01    Sept. 11, 2009   neilw        Fixed bug with creator (thanks brentg!)
    //    1.00    July 31, 2009    neilw        First published version
    //
    // Usage
    //    FeedTable(uri, entries?, content?)
    //
    //    uri:       uri of RSS2.0 feed
    //    entries:   max number of entries to display (default: 10).  0 => 100
    //    content:   "summary" = show content summary only
    //               "full"    = show full article content collapsed
    //               default   = show article titles only
    
    var uri     = $0 ?? $uri;
    var entries = $1 ?? $entries ?? 10;
    var content = $2 ?? $content;
    
    if (entries == 0) let entries = 100;
    
    var x = nil;
    if (uri == nil) { "ERROR: Must specify feed URI";<br />; }
    else let x = web.xml(uri);
    
    if (content != nil && content != "summary" && content != "full") {
        "ERROR: if specified, 'content' must be equal to 'full' or 'summary'";<br />;
        let content = nil;
    }
    
    if (x) <div class="plain"><table cellspacing="0" cellpadding="3" border="2" class="table">
        <thead><tr>
            <th style="padding: 5px; font-size: medium; font-weight: bold;"> web.link(xml.text(x,".//channel/link"), xml.text(x,".//channel/title")) </th>
        </tr></thead>
        <tbody><tr><td>
            foreach (var e in xml.select(x,'//item')) {
              if (__index < entries) <div>
                if (content=="summary" && __index > 0)
                    <hr style="border: medium none; color:rgb(221,221,221); background-color:rgb(221,221,221); height: 1px;" />;
                if (content == "full") collapseItem(@id..__index, _,_, "slide");
                var title = web.link(xml.text(e,".//link"),xml.text(e,".//title"));
                if (content) <strong> title </strong>;
                else title;
                <span style="color:rgb(128,128,128); font-size:smaller;"> " (posted "..string.substr(xml.text(e,".//pubDate"),0,16).." by "..
                    xml.text(e,".//dc:creator", { dc:"http://purl.org/dc/elements/1.1/" }) .. ")" </span>;
                if (content == "summary")
                    <p style="font-size: small;"> web.html(xml.text(e,".//description")) </p>;
                else if (content == "full")
                    <div id=(@id..__index) style="display:none; font-size:small;">
                        web.html(xml.text(e, ".//content:encoded", {content:"http://purl.org/rss/1.0/modules/content/"}));
                        <hr style="border: medium none; color:rgb(221,221,221); background-color:rgb(221,221,221); height: 1px;" />;
                    </div>;
              </div>;
            }
        </td></tr></tbody>
    </table></div>;
    Was this page helpful?
    Tag page
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by