Permalink
Browse files

TT renderer

  • Loading branch information...
1 parent a143bc4 commit 3569435b261dedc5640e80aa3f33f817e93d47d3 Zbigniew Lukasiak committed Jun 13, 2010
View
@@ -25,6 +25,7 @@ build_requires 'Config::Any';
build_requires 'Config::General';
build_requires 'HTML::FormHandler';
build_requires 'HTML::FormHandler::Model::DBIC';
+build_requires 'Template';
auto_install;
@@ -0,0 +1,41 @@
+package WebNano::Renderer::TT;
+use strict;
+use warnings;
+
+use Template;
+use Class::XSAccessor { accessors => [ qw/ root _tt include_path / ], constructor => 'new', };
+use File::Spec;
+
+sub _to_list {
+ if( ref $_[0] ){
+ return @{ $_[0] };
+ }
+ elsif( ! defined $_[0] ){
+ return ();
+ }
+ else{
+ return $_[0];
+ }
+}
+
+sub render {
+ my( $self, %params ) = @_;
+ my $template;
+ my @input_path = _to_list( $params{search_path} );
+ if( !@input_path ){
+ @input_path = ( '' );
+ }
+ $self->include_path([]);
+ for my $root( _to_list( $self->root ) ){
+ for my $sub_path( @input_path ){
+ push @{ $self->include_path }, File::Spec->catdir( $root, $sub_path );
+ }
+ }
+ if( !$self->_tt ){
+ $self->_tt( Template->new( INCLUDE_PATH => [ sub { $self->include_path } ] ) );
+ }
+ $self->_tt->process( $params{template}, $params{vars}, $params{output} );
+}
+
+1;
+
View
@@ -21,13 +21,10 @@ sub psgi_callback {
sub controller_search_path { [ ref(shift) ] };
-sub self_path { '' };
-sub application { shift };
-
sub handle {
my( $self, $env ) = @_;
my $path = $env->{PATH_INFO};
- my $c_class = $self->find_nested( '' );
+ my $c_class = $self->find_nested( '', $self->controller_search_path );
$path =~ s{^/}{};
my $out = $c_class->handle(
path => $path,
@@ -50,7 +50,7 @@ sub external_dispatch {
my( $path_part, $new_path ) = ( $path =~ qr{^([^/]*)/?(.*)} );
$path_part =~ s/::|'//g if defined( $path_part );
return if !length( $path_part );
- my $controller_class = $self->find_nested( $path_part );
+ my $controller_class = $self->find_nested( $self->self_path . $path_part, $self->application->controller_search_path );
return if !$controller_class;
return $controller_class->handle(
path => $new_path,
@@ -5,13 +5,13 @@ package WebNano::FindController;
use Try::Tiny;
sub find_nested {
- my( $self, $sub_path ) = @_;
+ my( $self, $sub_path, $search_path ) = @_;
$sub_path =~ s{::}{/};
my $controller_class;
- my @path = @{ $self->application->controller_search_path };
+ my @path = @$search_path;
for my $base ( @path ){
$base =~ s{::}{/}g;
- my $controller_file = "$base/Controller" . $self->self_path . "$sub_path.pm";
+ my $controller_file = "$base/Controller$sub_path.pm";
try{
require $controller_file;
$controller_class = $controller_file;
@@ -0,0 +1 @@
+tt1/some_template.tt
@@ -0,0 +1 @@
+tt1/subdir1/template.tt
@@ -0,0 +1 @@
+tt1/subdir2/template.tt
@@ -0,0 +1 @@
+tt1/subdir2/template1.tt
@@ -0,0 +1 @@
+tt2/second_root.tt
@@ -0,0 +1 @@
+tt2/some_template.tt
@@ -0,0 +1 @@
+tt2/subdir1/second_root.tt
@@ -0,0 +1 @@
+tt2/subdir2/second_root.tt
@@ -0,0 +1 @@
+tt2/subdir2/second_root1.tt
View
@@ -0,0 +1,42 @@
+use Test::More;
+use lib 't/lib';
+
+use lib 'extensions/lib';
+use WebNano::Renderer::TT;
+
+my $renderer = WebNano::Renderer::TT->new( root => [ 't/data/tt2' ] );
+
+my $out;
+$renderer->render( template => 'some_template.tt', output => \$out );
+is( $out, "tt2/some_template.tt\n" );
+
+$renderer = WebNano::Renderer::TT->new( root => [ 't/data/tt1', 't/data/tt2' ] );
+$out = '';
+$renderer->render( template => 'some_template.tt', output => \$out );
+is( $out, "tt1/some_template.tt\n" );
+$out = '';
+$renderer->render( template => 'second_root.tt', output => \$out );
+is( $out, "tt2/second_root.tt\n" );
+
+$out = '';
+$renderer->render( template => 'template.tt', search_path => [ 'subdir1', 'subdir2' ], output => \$out );
+is( $out, "tt1/subdir1/template.tt\n" );
+$out = '';
+$renderer->render( template => 'template.tt', search_path => [ 'subdir2' ], output => \$out );
+is( $out, "tt1/subdir2/template.tt\n" );
+$out = '';
+$renderer->render( template => 'template1.tt', search_path => [ 'subdir1', 'subdir2' ], output => \$out );
+is( $out, "tt1/subdir2/template1.tt\n" );
+
+$out = '';
+$renderer->render( template => 'second_root.tt', search_path => [ 'subdir1', 'subdir2' ], output => \$out );
+is( $out, "tt2/subdir1/second_root.tt\n" );
+$out = '';
+$renderer->render( template => 'second_root.tt', search_path => [ 'subdir2' ], output => \$out );
+is( $out, "tt2/subdir2/second_root.tt\n" );
+$out = '';
+$renderer->render( template => 'second_root1.tt', search_path => [ 'subdir1', 'subdir2' ], output => \$out );
+is( $out, "tt2/subdir2/second_root1.tt\n" );
+
+done_testing();
+

0 comments on commit 3569435

Please sign in to comment.