Skip to content

Commit

Permalink
globals templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Zbigniew Lukasiak committed Jun 15, 2010
1 parent 3569435 commit cbd5601
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
25 changes: 19 additions & 6 deletions extensions/lib/WebNano/Renderer/TT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@ use strict;
use warnings;

use Template;
use Class::XSAccessor { accessors => [ qw/ root _tt include_path / ], constructor => 'new', };
use Class::XSAccessor { accessors => [ qw/ root _tt global_path INCLUDE_PATH / ], };
use File::Spec;

sub new {
my( $class, %args ) = @_;
my $self = bless {
global_path => [ _to_list( delete $args{INCLUDE_PATH} ) ],
root => delete $args{root},
}, $class;
# Use a weakend copy of self so we dont have loops preventing GC from working
my $copy = $self;
Scalar::Util::weaken($copy);
$args{INCLUDE_PATH} = [ sub { $copy->INCLUDE_PATH } ];
$self->_tt( Template->new( \%args ) );
return $self;
}


sub _to_list {
if( ref $_[0] ){
return @{ $_[0] };
Expand All @@ -25,15 +40,13 @@ sub render {
if( !@input_path ){
@input_path = ( '' );
}
$self->include_path([]);
my @path = @{ $self->global_path };
for my $root( _to_list( $self->root ) ){
for my $sub_path( @input_path ){
push @{ $self->include_path }, File::Spec->catdir( $root, $sub_path );
push @path, File::Spec->catdir( $root, $sub_path );
}
}
if( !$self->_tt ){
$self->_tt( Template->new( INCLUDE_PATH => [ sub { $self->include_path } ] ) );
}
$self->INCLUDE_PATH( \@path );
$self->_tt->process( $params{template}, $params{vars}, $params{output} );
}

Expand Down
1 change: 1 addition & 0 deletions t/data/tt1/include_global.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[% INCLUDE some_global.tt %]
1 change: 1 addition & 0 deletions t/data/tt_globals/some_global.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t/data/tt_globals/some_global.tt
7 changes: 7 additions & 0 deletions t/tt_renderer.t
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,12 @@ $out = '';
$renderer->render( template => 'second_root1.tt', search_path => [ 'subdir1', 'subdir2' ], output => \$out );
is( $out, "tt2/subdir2/second_root1.tt\n" );

$renderer = WebNano::Renderer::TT->new( root => [ 't/data/tt1', 't/data/tt2' ], INCLUDE_PATH => 't/data/tt_globals' );
$out = '';
$renderer->render( template => 'include_global.tt', output => \$out );
is( $out, "t/data/tt_globals/some_global.tt\n\n" );



done_testing();

0 comments on commit cbd5601

Please sign in to comment.