$rss->as_javascript

LTjake on 2003-06-04T12:11:10

One of the benefits of RSS that I tried to convey to others in the office is the fact that anyone can take our feeds and parse them - thus an extra "broadcast" method. This is fine for people with some programming knowledge, or perhaps their CMS, aggregator, or whatever has the ability to parse RSS. For those people who just have plain old HTML pages the only way to include a feed seems to be through a javascript include:

<script language="JavaScript" src="http://host.com/feeds/myfeed.js"></script>
<noscript><a href="http://host.com/">My Feed Thing</a><noscript>

The only downside is that if javascript is disabled, you'll only get a link rather than the feed.

Since I'm using XML::RSS to construct a feed, i subclassed it and added two new output methods as_javascript and save_javascript. The second simply calling the first and saving the results to a file. There is a "max" parameter in case you include too many items in your feed for a reasonably lengthed html display.

package XML::RSS::JavaScript;

use strict;
use Carp;
use base 'XML::RSS';

sub save_javascript {
   my ( $self, $file, $max ) = @_;
   open( OUT, ">$file" ) || croak "Cannot open file $file for write: $!";
       print OUT $self->as_javascript( $max );
   close OUT;    
}

sub as_javascript {
   my ( $self, $max ) = @_;

   # Update - 2003-06-12
   # Fixed $max incase a max higher than the number of items is used
   my $items = scalar @{ $self->{ items } };

   if ( not $max or $max > $items ) {
       $max = $items;
   }
   ###

   my $left = "document.write('";
   my $right = "');\n";

   my $output;

   $output .= $left . '<p class="feed_title">' . $self->channel( 'title' ) . '</p>' . $right;
   $output .= $left . '<p class="feed_body">' . $right;

   foreach my $item ( ( @{ $self->{ items } } )[ 0..$max - 1 ] ) {
       $output .= $left . '<a href="' . $item->{ link } . '">' . $item->{ title } . '</a><br />' . $right;
   }

   $output .= $left . '</p>' . $right;

   return $output;
}

1;

The output is purely textual, but you could change it to output the feed image instead of the title. Also, rather than having a paragraph tag and break separated links, you might consider an unordered list. You can do some fancy stuff with lists in CSS.


XML::RSS::JavaScript

aaron of montreal on 2003-06-04T15:51:14

Not sure if this fits in to your plans, but FYI:

xml-rss.js

The example page is busted (why? who knows?) but it gives you an idea of how it's supposed to work.

Re:XML::RSS::JavaScript

LTjake on 2003-06-04T17:37:06

Hi,

Thanks for the link, but, there doesn't seem to be any major advantage to that method. It still requires javascript, but now it has to parse the feed too. Rather, i process the feed on the server side, so all you have to include one file with a bunch of document.write() statements in it.

It's certainly advantageous for people who want to customise what is returned from the feed rather than getting what i choose to give them. But for non-techies, I'm not sure that's viable (or AS viable).