Permalink
Browse files

globals templates

  • Loading branch information...
1 parent 3569435 commit cbd560105fd1adb807fdeb6dd4e4f129a66d90bd Zbigniew Lukasiak committed Jun 15, 2010
Showing with 28 additions and 6 deletions.
  1. +19 −6 extensions/lib/WebNano/Renderer/TT.pm
  2. +1 −0 t/data/tt1/include_global.tt
  3. +1 −0 t/data/tt_globals/some_global.tt
  4. +7 −0 t/tt_renderer.t
@@ -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] };
@@ -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} );
}
@@ -0,0 +1 @@
+[% INCLUDE some_global.tt %]
@@ -0,0 +1 @@
+t/data/tt_globals/some_global.tt
View
@@ -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.