2010-07-19 15 views
1

我試圖創建一個將Text :: Haml集成到模板工具箱的cpan模塊。我認爲Haml是一個有趣的模板語言,但相當有限,它不支持循環或條件,更不用說更高級的東西。但我似乎無法得到一個非常簡單的版本去工作。以下是一些測試腳本,可以確保我的邏輯工作。試圖爲Haml創建Template :: Plugin :: Filter過濾器

下面是一個使用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.

我知道這是什麼錯誤意味着...但我不知道爲什麼過濾器造成這種情況。任何幫助,將不勝感激

以下是一些測試腳本,我的邏輯工作的其他部分,以便我們沒有看錯代碼來解決問題。否則,他們是不是真正的問題需要

這說明了如何使用模板::工具包

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

這向我們展示瞭如何使用文本:: 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> 

(更新版)

我試過這個(這幾乎和markdown插件完全相同,而且幾乎與紡織插件完全相同從實現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 ([email protected]) { 
     $_tt_error = $context->catch([email protected], \$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 %] ##</> 
+0

'使用Template :: Plugin :: Filter'是多餘的,''使用父母......'本身就很好 – singingfish 2010-07-20 00:14:25

+0

我有點想到太...不知道爲什麼文檔有這種方式。 – xenoterracide 2010-07-20 00:26:12

+0

以及它看起來像文檔是錯誤的。我會通過IRC頻道的支持來看看這個(在irc.perl.org上的#tt btw) – singingfish 2010-07-20 00:58:15

回答

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',我需要一些初始化代碼。

0

OK

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 ,我不能得到這個工作的。

細化測試腳本有所消除可能分心:

#!/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位,然後我得到的全部輸出爲期望太高)