【发布时间】:2012-12-03 13:24:16
【问题描述】:
我正在编写一个脚本来将我的 Stackoverflow 活动提要拉入网页,它看起来像这样:
#!/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();
模板应该迭代我的活动提要(来自XML::Feed->items())并打印每个的标题。当我运行这段代码时,我得到:
var.undef error - undefined variable: items
为了让它工作,我必须将process 行更改为:
$tt->process( \$template, { 'items' => [ $feed->items ] } )
谁能解释为什么Template::Toolkit 似乎无法使用XML::Feed->items() 方法?
我与XML::RSS 有过类似的工作:
my $rss = XML::RSS->new();
$rss->parse($feed);
$tt->process ( \$template, $rss )
or die $tt->error();
【问题讨论】:
标签: xml perl atom-feed template-toolkit