3

I'm writing a script to pull my Stackoverflow activity feed into a webpage, it looks something like this:

#!/usr/bin/perl

use strict;
use warnings;

use XML::Feed;
use Template;

my $stackoverflow_id = 1691146;
my $stackoverflow_url = "http://stackoverflow.com/feeds/user/$stackoverflow_id";

my $template = <<'TEMPLATE';
[% FOREACH item = items %]
[% item.title %]
[% END %]
TEMPLATE

my $tt = Template->new( 'STRICT' => 1 )
  or die "Failed to load template: $Template::ERROR\n";

my $feed = XML::Feed->parse(URI->new($stackoverflow_url));

$tt->process( \$template, $feed )
  or die $tt->error();

The template should iterate my activity feed (from XML::Feed->items()) and print the title of each. When I run this code I get:

var.undef error - undefined variable: items

To get it to work I had to change the process line to:

$tt->process( \$template, { 'items' => [ $feed->items ] } )

Can anyone explain why Template::Toolkit appears unable to use the XML::Feed->items() method?

I've had something similar working with XML::RSS:

my $rss = XML::RSS->new();
$rss->parse($feed);
$tt->process ( \$template, $rss )
    or die $tt->error();

1 Answer 1

3

Just a couple of tweaks.

#!/usr/bin/perl -Tw

use strict;
use warnings;

use XML::Feed;
use Template;
use Data::Dumper;

my $stackoverflow_id = 1691146;
my $stackoverflow_url = "http://stackoverflow.com/feeds/user/$stackoverflow_id";

my $template = <<'TEMPLATE';
[% FOREACH item = feed.items() %]
[% item.title %]
[% END %]
TEMPLATE

my $tt = Template->new( 'STRICT' => 1 )
  or die "Failed to load template: $Template::ERROR\n";

my $feed = XML::Feed->parse(URI->new($stackoverflow_url));



$tt->process( \$template, { feed => $feed } )
  or die $tt->error();

The template compiler expects a plain hash ref, the keys and values of which are stored internally. Giving it an XML::RSS object works as it has an items element. An XML::Feed object doesn't have an items element as it is just a wrapper for several implementations (including XML::RSS). The template wouldn't get an XML::Feed object, it'd get a plain hash ref, something like:

{ 'rss' => XML::RSS Object }

Wrapping your feed in a hash ref makes the compiler retain the XML::Feed object, allowing the processing engine to perform the 'magic' required when feed.items is found in the template.

1
  • @RobEarl, the template compiler needs a hash ref with a key pointing at the $feed object. From there it has no problem calling the items method.
    – ddoxey
    Dec 16, 2012 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.