Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

render with named params; static INCLUDE_PATH

  • Loading branch information...
commit 9a01c72a9e1521a78551927c974dee94081b145f 1 parent f69c439
Zbigniew Lukasiak authored
14 extensions/WebNano-Renderer-TT/lib/WebNano/Renderer/TT.pm
View
@@ -35,8 +35,12 @@ sub _to_list {
sub render {
my( $self, %params ) = @_;
- my $template;
- my @input_path = _to_list( $params{search_path} );
+ my $c = $params{c};
+ my @input_path;
+ if( $c ){
+ @input_path = $c->self_path;
+ push @input_path, @{ $c->template_search_path } if $c->template_search_path;
+ }
if( !@input_path ){
@input_path = ( '' );
}
@@ -48,8 +52,10 @@ sub render {
}
$self->INCLUDE_PATH( \@path );
my $tt = $self->_tt;
- $tt->process( $params{template}, $params{vars}, $params{output} )
- || die $tt->error();;
+ my $output;
+ $tt->process( $params{template}, \%params, \$output )
+ || die $tt->error();
+ return $output;
}
1;
40 extensions/WebNano-Renderer-TT/t/tt_renderer.t
View
@@ -3,44 +3,44 @@ use lib 't/lib';
use lib 'extensions/lib';
use WebNano::Renderer::TT;
+{
+ package TestController;
+ use base 'WebNano::Controller';
+ use Object::Tiny::RW 'template_search_path';
+}
my $renderer = WebNano::Renderer::TT->new( root => [ 't/data/tt2' ] );
-my $out;
-$renderer->render( template => 'some_template.tt', output => \$out );
+my $out = $renderer->render( template => 'some_template.tt' );
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 );
+$out = $renderer->render( template => 'some_template.tt' );
is( $out, "tt1/some_template.tt\n" );
-$out = '';
-$renderer->render( template => 'second_root.tt', output => \$out );
+$out = $renderer->render( template => 'second_root.tt' );
is( $out, "tt2/second_root.tt\n" );
-$out = '';
-$renderer->render( template => 'template.tt', search_path => [ 'subdir1', 'subdir2' ], output => \$out );
+my $c = TestController->new( self_path => 'subdir1', template_search_path => [ 'subdir2' ] );
+$out = $renderer->render( c => $c, template => 'template.tt' );
is( $out, "tt1/subdir1/template.tt\n" );
-$out = '';
-$renderer->render( template => 'template.tt', search_path => [ 'subdir2' ], output => \$out );
+my $c = TestController->new( self_path => 'subdir2' );
+$out = $renderer->render( c => $c, template => 'template.tt' );
is( $out, "tt1/subdir2/template.tt\n" );
-$out = '';
-$renderer->render( template => 'template1.tt', search_path => [ 'subdir1', 'subdir2' ], output => \$out );
+my $c = TestController->new( self_path => 'subdir1', template_search_path => [ 'subdir2' ] );
+$out = $renderer->render( c => $c, template => 'template1.tt' );
is( $out, "tt1/subdir2/template1.tt\n" );
-$out = '';
-$renderer->render( template => 'second_root.tt', search_path => [ 'subdir1', 'subdir2' ], output => \$out );
+$out = $renderer->render( c => $c, template => 'second_root.tt' );
is( $out, "tt2/subdir1/second_root.tt\n" );
-$out = '';
-$renderer->render( template => 'second_root.tt', search_path => [ 'subdir2' ], output => \$out );
+my $c = TestController->new( self_path => 'subdir2' );
+$out = $renderer->render( c => $c, template => 'second_root.tt' );
is( $out, "tt2/subdir2/second_root.tt\n" );
-$out = '';
-$renderer->render( template => 'second_root1.tt', search_path => [ 'subdir1', 'subdir2' ], output => \$out );
+my $c = TestController->new( self_path => 'subdir1', template_search_path => [ 'subdir2' ] );
+$out = $renderer->render( c => $c, template => 'second_root1.tt' );
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 );
+$out = $renderer->render( template => 'include_global.tt' );
is( $out, "t/data/tt_globals/some_global.tt\n\n" );
4 lib/WebNano/Controller.pm
View
@@ -25,8 +25,8 @@ sub request {
sub template_search_path { [] }
sub render {
- my ( $self, $template, $vars ) = @_;
- return $self->application->renderer->render( $self, $template, $vars );
+ my $self = shift;
+ return $self->application->renderer->render( c => $self, @_ );
}
sub external_dispatch {
47 lib/WebNano/Renderer/TTiny.pm
View
@@ -3,7 +3,7 @@ use strict;
use warnings;
use Template::Tiny;
-use Object::Tiny::RW qw/ root _tt_tiny /;
+use Object::Tiny::RW qw/ root _tt_tiny INCLUDE_PATH /;
use File::Spec;
@@ -20,46 +20,61 @@ sub _to_list {
}
sub render {
- my( $self, $c, $template, $vars ) = @_;
- $vars ||= {};
- $vars->{self_url} = $c->self_url;
- $vars->{c} = $c;
+ my( $self, %vars ) = @_;
+ my $c = $vars{c};
- my $path = $c->self_path;
- $path =~ s{^/}{};
- my @search_path = ( $path, @{ $c->template_search_path });
+ my @search_path;
+ if( $c ){
+ my $path = ref $c;
+ $path =~ s/.*::Controller(::)?//;
+ $path =~ s{::}{/};
+ @search_path = ( $path, @{ $c->template_search_path });
+ }
if( !@search_path ){
@search_path = ( '' );
}
- my $full_template;
+ my $template;
LOOP:
for my $path ( @search_path ){
my $to_check;
if( !$self->root || File::Spec->file_name_is_absolute( $path ) ){
- $to_check = File::Spec->catfile( $path, $template );
+ $to_check = File::Spec->catfile( $path, $vars{template} );
if( -f $to_check ){
- $full_template = $to_check;
+ $template = $to_check;
last LOOP;
}
}
else{
for my $root ( _to_list( $self->root ) ){
- $to_check = File::Spec->catfile( $root, $path, $template );
+ $to_check = File::Spec->catfile( $root, $path, $vars{template} );
if( -f $to_check ){
- $full_template = $to_check;
+ $template = $to_check;
last LOOP;
}
}
}
}
- die "Cannot find $template in search path: @search_path" if !defined $full_template;
- open my $fh, $full_template or die "Cannot read from $full_template: $!";
+ my @static_search_path;
+ if( !$template ){
+ @static_search_path = _to_list( $self->INCLUDE_PATH );
+ STATIC_LOOP:
+ for my $path ( @static_search_path ){
+ my $to_check;
+ $to_check = File::Spec->catfile( $path, $vars{template} );
+ if( -f $to_check ){
+ $template = $to_check;
+ last STATIC_LOOP;
+ }
+ }
+ }
+ die "Cannot find $vars{template} in search path: @search_path, @static_search_path" if !defined $template;
+ open my $fh, $template or die "Cannot read from $template: $!";
my $string = do { local $/; <$fh> };
if( !$self->_tt_tiny ){
$self->_tt_tiny( Template::Tiny->new() );
}
my $out;
- $self->_tt_tiny->process( \$string, $vars, \$out );
+ $self->_tt_tiny->process( \$string, \%vars, \$out );
return $out;
}
7 t/10.main.t
View
@@ -1,3 +1,6 @@
+use strict;
+use warnings;
+
use Test::More;
use Plack::Test;
use HTTP::Request::Common;
@@ -8,8 +11,8 @@ use WebNano::Renderer::TTiny;
use WebNano::Controller;
my $c = WebNano::Controller->new( self_path => '', template_search_path => [] );
-my $dt = WebNano::Renderer::TTiny->new( root => 't/data/templates' );
-my $rendered = $dt->render( $c, 'dummy_template', { some_var => 'some value' } );
+my $renderer = WebNano::Renderer::TTiny->new( root => 't/data/templates', application => WebNano->new() );
+my $rendered = $renderer->render( c => $c, template => 'dummy_template', some_var => 'some value' );
ok( $rendered =~ /some_var: some value/, 'vars' );
ok( $rendered =~ /^Some text/, 'Slurping template file' );
2  t/lib/MyApp/Controller.pm
View
@@ -15,7 +15,7 @@ sub new {
sub index_action {
my $self = shift;
- return $self->render( 'index.tt' );
+ return $self->render( template => 'index.tt' );
}
sub mapped_url { 'This is the mapped url page' }
2  t/lib/MyApp/Controller/NestedController.pm
View
@@ -15,7 +15,7 @@ sub safe_method { 'This is the safe_method page' }
sub some_method_action { 'This is a method with _action postfix' }
-sub with_template_action { shift->render( 'some_template' ) }
+sub with_template_action { shift->render( template => 'some_template' ) }
1;
2  t/lib/MyApp/Controller/NestedController2.pm
View
@@ -7,7 +7,7 @@ use base 'WebNano::Controller';
sub some_method_action { 'This is a method with _action postfix in MyApp::Controller::NestedController2' }
-sub with_template_action { shift->render( 'some_template' ) }
+sub with_template_action { shift->render( template => 'some_template' ) }
1;
6 t/lib/MyApp/Controller/Product.pm
View
@@ -10,9 +10,9 @@ sub template_search_path {
return [ 'Product', @{ $self->SUPER::template_search_path( @_ ) } ];
}
-sub some_action { shift->render( 'some_template' ) }
-sub another_action { shift->render( 'another_template' ) }
-sub third_action { shift->render( 'third_template' ) }
+sub some_action { shift->render( template => 'some_template' ) }
+sub another_action { shift->render( template => 'another_template' ) }
+sub third_action { shift->render( template => 'third_template' ) }
1;
34 t/renderer.t
View
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+
+use Test::More;
+use lib 't/lib';
+use WebNano::Renderer::TTiny;
+use WebNano::Controller;
+
+{
+ package WebNano::Controller::Book;
+ use base 'WebNano::Controller';
+ sub template_search_path { [ 'Product' ] }
+}
+
+my $c = WebNano::Controller->new();
+my $renderer = WebNano::Renderer::TTiny->new( root => 't/data/templates' );
+my $rendered = $renderer->render( c => $c, template => 'dummy_template', some_var => 'some value' );
+ok( $rendered =~ /some_var: some value/, 'vars' );
+ok( $rendered =~ /^Some text/, 'Slurping template file' );
+
+$c = WebNano::Controller::Book->new();
+like(
+ $renderer->render( c => $c, template => 'third_template', some_var => 'some value' ),
+ qr/This is template for Book/
+);
+
+my $static_renderer = WebNano::Renderer::TTiny->new( INCLUDE_PATH => 't/data/templates' );
+like(
+ $static_renderer->render( c => $c, template => 'Book/third_template', some_var => 'some value' ),
+ qr/This is template for Book/
+);
+
+
+done_testing();
Please sign in to comment.
Something went wrong with that request. Please try again.