2010-07-19 5 views
1

Text :: Haml을 Template Toolkit에 통합하는 cpan 모듈을 만들려고합니다. Haml은 흥미로운 템플릿 언어이지만 다소 제한적이라고 생각합니다. 루프 나 조건문을 사용하면 더 많은 고급 작업을 수행 할 수 있습니다. 그러나 나는 아주 단순한 버전의 작업을 할 수없는 듯합니다. 다음은 내 논리가 작동하는지 확인하기 위해 작동하는 일부 테스트 스크립트입니다.Haml 용 Template :: Plugin :: Filter 만들기 시도

는 여기

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

를 사용하도록 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; 

일부 코드를 사용하여 필터 모듈에서 내 시도이다 그러나 나는 Use of uninitialized value in print at test0.pl line 15, <DATA> line 1.

내가 무엇을이 오류를 알고이 경고를 얻을 의미 ...하지만 왜 필터가 이것을 일으키는 지 잘 모르겠습니다. 어떤 도움을 주시면 감사하겠습니다.

다음은 내 논리의 다른 부분이 작동하는 몇 가지 테스트 스크립트로, 문제를 해결하기 위해 잘못된 코드를 찾지 않았습니다. 그렇지 않으면 그들은 문제에 대한 정말 필요하지 않습니다

이이 텍스트 :: 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을 사용하는 방법을 우리에게 보여줍니다 템플릿 :: 툴킷

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

을 사용하는 방법을 보여줍니다

나는 markdown 플러그인과 거의 동일하며 Textile Plugin과 거의 동일하다. 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

'Use Template :: Plugin :: Filter'는 중복되어'use parent ...'자체가 괜찮습니다. – singingfish

+0

나는 다소 그렇게 생각 했었습니다. – xenoterracide

+0

글쎄, 워드 프로세서가 잘못된 것처럼 보입니다. IRC 채널의 지원을 받아 보겠습니다 (irc.perl.org btw의 #tt). – singingfish

답변

1
: 나는 템플릿의 HAML 블록하지 않고이 작업을 실행하지만 만약 내가 출력을 얻을

여기

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

확인을 DEBUG => 'all', 가능)에서

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 출력, I 중 하나가 작동이를 얻을 수 없습니다.

#!/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 그것을 실행할 때 나는 어떤 출력을 얻을 (A CPAN의 DIST를 설정 한) :

어느 정도 가능 산만을 제거하기 위해 테스트 스크립트를 정련.

Some non haml stuff 

(필자는 필터 HAML과 END 비트를 주석 경우도 예상대로, 나는 전체 출력을 얻을)