我正在尝试创建一个将 Text::Haml 集成到模板工具包中的 cpan 模块。我认为 Haml 是一种有趣的模板语言,但相当有限,它不支持循环或条件,更不用说更高级的东西了。但我似乎无法得到一个非常简单的版本来工作。以下是一些可以确保我的逻辑正常工作的测试脚本。
这是我使用Template::Plugin::Filter文档对过滤器模块的尝试
use strict;
use warnings;
package Template::Plugin::Haml;
use Template::Plugin::Filter;
use parent 'Template::Plugin::Filter';
sub filter {
my ( $self, $text ) = @_;
# thes aren't actually the problem
# my $haml = Text::Haml->new;
# my $html = $haml->render($text);
# return $html;
return $text;
}
1;
以及一些使用它的代码
#!/usr/bin/perl
# test0.pl
use strict;
use warnings;
use Template;
my $tt = Template->new;
my $vars = {};
my $output = \do{my $i};
$tt->process(\*DATA, $vars, $output);
print $$output;
__DATA__
[% USE Haml %]
[% FILTER Haml %]
#profile
[% END %]
但我收到了这个警告Use of uninitialized value in print at test0.pl line 15, <DATA> line 1.
我知道这个错误意味着什么......但我不确定为什么过滤器会导致这个错误。任何帮助,将不胜感激
以下是我的逻辑的其他部分工作的一些测试脚本,这样我们就不会查看错误的代码来解决问题。否则,它们对于问题并不是真正必要的
这显示了如何使用 Template::Toolkit
#!/usr/bin/perl
# test1.pl - show how to use tt
use strict;
use warnings;
use Template;
my $tt = Template->new;
my $vars = {};
my $output = \do{my $i};
$tt->process(\*DATA, $vars, $output);
print $$output; # #profile
__DATA__
#profile
这向我们展示了如何使用 Text::Haml
#!/usr/bin/perl
# test2.pl
use 5.010;
use strict;
use warnings;
use Text::Haml;
my $text = '#profile';
my $haml = Text::Haml->new;
my $html = $haml->render($text);
say $html; # <div id='profile'></div>
更新 1
我试过这个(它几乎与降价插件相同,也几乎与 Textile 插件相同)
use strict;
use warnings;
package Template::Plugin::Haml;
use parent 'Template::Plugin::Filter';
use 'Text::Haml';
sub init {
my $self = shift;
$self->{_DYNAMIC} = 1;
$self->install_filter( $self->{_ARGS}->[0] || 'haml');
$self;
}
sub filter {
my ( $self, $text, $args, $config ) = @_;
my $haml = Text::Haml->new;
return $haml->render($text);
}
1;
DEBUG => 'all',
在 TT 初始化时启用的UPDATE 2
输出
[Template::Provider] creating cache of unlimited slots for [ . ]
[Template::Service] process(GLOB(0x1719608), HASH(0x16f1650))
[Template::Context] template(GLOB(0x1719608))
[Template::Context] asking providers for [GLOB(0x1719608)] []
[Template::Provider] _load(GLOB(0x1719608), <no alias>)
[Template::Provider] _compile(HASH(0x1a947a0), <no compfile>)
[Template::Parser] compiled main template document block:
sub {
my $context = shift || die "template sub called without context\n";
my $stash = $context->stash;
my $output = '';
my $_tt_error;
eval { BLOCK: {
#line 1 "input file handle"
$output .= $context->debugging('msg', { 'line' => '1', 'text' => 'USE Haml', 'file' => 'input file handle' }); ## DEBUG ##
#line 1 "input file handle"
# USE
$stash->set('Haml',
$context->plugin('Haml'));
#line 2 "input file handle"
$output .= $context->debugging('msg', { 'line' => '2', 'text' => 'FILTER haml', 'file' => 'input file handle' }); ## DEBUG ##
#line 4 "input file handle"
# FILTER
$output .= do {
my $output = '';
my $_tt_filter = $context->filter('haml')
|| $context->throw($context->error);
$output .= "#profile";
#line 4 "input file handle"
$output .= $context->debugging('msg', { 'line' => '4', 'text' => 'END', 'file' => 'input file handle' }); ## DEBUG ##
&$_tt_filter($output);
};
} };
if ($@) {
$_tt_error = $context->catch($@, \$output);
die $_tt_error unless $_tt_error->type eq 'return';
}
return $output;
}
[Template::Service] PROCESS: Template::Document=HASH(0x1c69ba0)
[Template::Context] process([ Template::Document=HASH(0x1c69ba0) ], <no params>, <unlocalized>)
[Template::Context] template(Template::Document=HASH(0x1c69ba0))
[Template::Context] plugin(Haml, [ ])
[Template::Plugins] fetch(Haml, <no args>, Template::Context=HASH(0x1972040))
[Template::Plugins] loading Template/Plugin/Haml.pm (PLUGIN_BASE)
[Template::Plugins] calling Template::Plugin::Haml->load()
[Template::Plugins] Haml => Template::Plugin::Haml
[Template::Filters] store(haml, ARRAY(0x1c1f4d8))
[Template::Context] filter(haml, [ ]<no alias>)
[Template::Filters] fetch(haml, <no args>, Template::Context=HASH(0x1972040))
Use of uninitialized value in string eq at /usr/share/perl5/vendor_perl/Text/Haml.pm line 452, <DATA> line 1.
Use of uninitialized value in string eq at /usr/share/perl5/vendor_perl/Text/Haml.pm line 452, <DATA> line 1.
Use of uninitialized value in string eq at /usr/share/perl5/vendor_perl/Text/Haml.pm line 452, <DATA> line 1.
Use of uninitialized value in string eq at /usr/share/perl5/vendor_perl/Text/Haml.pm line 452, <DATA> line 1.
Use of uninitialized value in string eq at /usr/share/perl5/vendor_perl/Text/Haml.pm line 452, <DATA> line 1.
Use of uninitialized value in string eq at /usr/share/perl5/vendor_perl/Text/Haml.pm line 452, <DATA> line 1.
Use of uninitialized value in string eq at /usr/share/perl5/vendor_perl/Text/Haml.pm line 452, <DATA> line 1.
Use of uninitialized value in string eq at /usr/share/perl5/vendor_perl/Text/Haml.pm line 452, <DATA> line 1.
Use of uninitialized value in string eq at /usr/share/perl5/vendor_perl/Text/Haml.pm line 452, <DATA> line 1.
Use of uninitialized value in string eq at /usr/share/perl5/vendor_perl/Text/Haml.pm line 452, <DATA> line 1.
Use of uninitialized value in concatenation (.) or string at /usr/share/perl5/vendor_perl/Text/Haml.pm line 674, <DATA> line 1.
Use of uninitialized value in concatenation (.) or string at /usr/share/perl5/vendor_perl/Text/Haml.pm line 683, <DATA> line 1.
## input file handle line 1 : [% USE Haml %] ##
## input file handle line 2 : [% FILTER haml %] ##
<div id='profile'></div>
<>## input file handle line 4 : [% END %] ##</>