1

Text::Haml を Template Toolkit に統合する 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

私はこれを試しました(これは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;

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

2 に答える 2

1

CPAN Template::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'したようで、初期化コードが必要だったようです。

于 2010-07-20T02:55:24.907 に答える
0

OK、これも機能しません。

気を散らす可能性のあるものを取り除くために、テスト スクリプトを多少改良します。

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

(フィルター HAML と END ビットをコメントアウトすると、期待どおりの完全な出力も得られます)

于 2010-07-20T00:34:15.273 に答える