尝试为 Haml 创建 Template::Plugin::Filter

发布于 2024-09-10 16:44:40 字数 6352 浏览 5 评论 0原文

我正在尝试创建一个将 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,第 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>

UPDATE 1

我已经尝试过这个(这几乎与markdown 插件和一个几乎与 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;

更新 2 启用 DEBUG => 的输出'all', 在 TT 初始化时

[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 %] ##</>

I'm trying to create a cpan module that integrates Text::Haml into Template Toolkit. I think that Haml is an interesting templating language but rather limited, it doesn't support loops or conditionals let alone more advanced things. But I can't seem to get eve a very simple version to work. The following are some test scripts that work to make sure my logic works.

Here's my attempt at the filter module using the Template::Plugin::Filter Documentation

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;

and some code to use it

#!/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 %]

but I get this warning Use of uninitialized value in print at test0.pl line 15, <DATA> line 1.

I know what this error means... but I'm not sure why the filter causing this. Any help would be appreciated

The following are some test scripts that other parts of my logic work, so that we're not looking at the wrong code to fix the issue. Otherwise they aren't really necessary for the problem

This shows how to use 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

this shows us how to use 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>

UPDATE 1

I've tried this (which is almost identical to the markdown plugin and one almost identical to the Textile plugin as well)

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;

UPDATE 2
output from enabling DEBUG => 'all', at TT initialization

[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 %] ##</>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

去了角落 2024-09-17 16:44:41

以下是 CPAN 最终产品的链接 模板::Plugin::Haml

明白了

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;

,test0.pl

#!/usr/bin/perl
# test0.pl
use strict;
use warnings;

use Template;

my $tt = Template->new; #or die $Template::Error, "\n";

my $vars = {};
my $output = \do{my $i};

$tt->process(\*DATA, $vars, $output);

print $output; # \n\n<div id='profile'></div>\n\n
__DATA__
[% USE Haml %]
[% FILTER haml %]
#profile
[% END %]

似乎我引用了 use 'Text::Haml' 并且我需要一些初始化代码。

Here's a link to the final product on CPAN Template::Plugin::Haml

Got it

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;

and test0.pl

#!/usr/bin/perl
# test0.pl
use strict;
use warnings;

use Template;

my $tt = Template->new; #or die $Template::Error, "\n";

my $vars = {};
my $output = \do{my $i};

$tt->process(\*DATA, $vars, $output);

print $output; # \n\n<div id='profile'></div>\n\n
__DATA__
[% USE Haml %]
[% FILTER haml %]
#profile
[% END %]

seems I had quoted use 'Text::Haml' and that I needed some init code.

绝影如岚 2024-09-17 16:44:41

好吧,我也无法让它工作。

稍微改进测试脚本以消除可能的干扰:

#!/usr/bin/env perl
use strict;
use warnings;
use FindBin qw/$Bin/;
use lib "$Bin/../lib";

use Template;
my $tt = Template->new(
    STRICT => 1,
    PLUGINS => { MyFilter => 'Template::Plugin::Haml'},
    );

$tt->process(\*DATA, {});

__DATA__
Some non haml stuff
[% USE Haml %]
[% FILTER Haml %]
%h1. some haml stuff
[% END %]

当我使用 perl -Ilib t/01-test.t 运行它时(设置了 cpan dist),我没有得到任何输出。但是,如果我在模板中没有 HAML 块的情况下运行此命令,我会得到输出:(

Some non haml stuff

如果我注释掉 Filter HAML 和 END 位,那么我也会得到预期的完整输出)

OK, I can't get this to work either.

Refining the test script somewhat to remove possible distractions:

#!/usr/bin/env perl
use strict;
use warnings;
use FindBin qw/$Bin/;
use lib "$Bin/../lib";

use Template;
my $tt = Template->new(
    STRICT => 1,
    PLUGINS => { MyFilter => 'Template::Plugin::Haml'},
    );

$tt->process(\*DATA, {});

__DATA__
Some non haml stuff
[% USE Haml %]
[% FILTER Haml %]
%h1. some haml stuff
[% END %]

And when I run it with perl -Ilib t/01-test.t (having set up a cpan dist) I get no output. However if I run this without the HAML block in the template I get the output:

Some non haml stuff

(If I comment out the Filter HAML and the END bit, then I get the full output as expected too)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文