Permalink
Browse files

finding template based on caller in TT

  • Loading branch information...
1 parent 95ab491 commit 0073a32f04ac516a1333521f73e8f087ea8d3874 Zbigniew Lukasiak committed Aug 28, 2010
@@ -3,7 +3,7 @@ use strict;
use warnings;
use Template;
-use Object::Tiny::RW qw/ root _tt global_path INCLUDE_PATH /;
+use Object::Tiny::RW qw/ root _tt global_path INCLUDE_PATH TEMPLATE_EXTENSION/;
use File::Spec;
sub new {
@@ -53,9 +53,17 @@ sub render {
}
}
$self->INCLUDE_PATH( \@path );
+ my $template = $params{template};
+ if( !$template ){
+ my @caller = caller(2);
+ $template = $caller[3];
+ $template =~ s/_action$//;
+ $template =~ s/^.*:://;
+ $template .= '.' . $self->TEMPLATE_EXTENSION if $self->TEMPLATE_EXTENSION;
+ }
my $tt = $self->_tt;
my $output;
- $tt->process( $params{template}, \%params, \$output )
+ $tt->process( $template, \%params, \$output )
|| die $tt->error();
return $output;
}
@@ -13,6 +13,16 @@ use WebNano::Renderer::TT;
{
package MyApp::Controller::subdir2;
use base 'WebNano::Controller';
+
+ sub some_action {
+ my( $self, $renderer ) = @_;
+ return $self->render( $renderer );
+ }
+
+ sub render {
+ my( $self, $renderer ) = @_;
+ return $renderer->render( c => $self );
+ }
}
my $renderer = WebNano::Renderer::TT->new( root => [ 't/data/tt2' ] );
@@ -49,7 +59,10 @@ $renderer = WebNano::Renderer::TT->new( root => [ 't/data/tt1', 't/data/tt2' ],
$out = $renderer->render( template => 'include_global.tt' );
is( $out, "t/data/tt_globals/some_global.tt\n\n" );
-
+is(
+ MyApp::Controller::subdir2->new()->some_action( $renderer ),
+ "This is 'some' template in 't/data/tt1/subdir2'\n"
+);
done_testing();

0 comments on commit 0073a32

Please sign in to comment.