Browse files

First attempt at template search path and inheritance

  • Loading branch information...
1 parent 9d48712 commit ee7d03775bd5674829874e571f9f3394aeb0f138 Zbigniew Lukasiak committed May 23, 2010
View
6 lib/WebNano/Controller.pm
@@ -12,7 +12,7 @@ use Class::XSAccessor {
constructor => 'new'
};
-sub template_dir {
+sub my_dir {
my ( $self ) = @_;
my $dir = ref $self;
my $prefix = ref $self->application;
@@ -23,6 +23,8 @@ sub template_dir {
return $dir;
}
+sub template_search_path { [] }
+
sub render {
my ( $self, $template, $vars ) = @_;
my $out;
@@ -31,7 +33,7 @@ sub render {
$vars->{self_url} = $self->self_url;
if( $t->render(
template => $template,
- search_path => $self->template_dir,
+ search_path => [ $self->my_dir, @{ $self->template_search_path } ],
vars => $vars,
output => \$out
)
View
14 t/10.main.t
@@ -30,6 +30,20 @@ test_psgi(
$res = $cb->(GET "SubController/with_template");
like( $res->content, qr/This is a SubController page rendered with a template/ );
+ $res = $cb->(GET "Product/some");
+ like( $res->content, qr/This is the example template for ControllerWithTemplates/ );
+ $res = $cb->(GET "Product/another");
+ like( $res->content, qr/This is template for Product/ );
+ $res = $cb->(GET "Product/third");
+ like( $res->content, qr/This is template for Product/ );
+
+ $res = $cb->(GET "Book/some");
+ like( $res->content, qr/This is the example template for ControllerWithTemplates/ );
+ $res = $cb->(GET "Book/another");
+ like( $res->content, qr/This is template for Product/ );
+ $res = $cb->(GET "Book/third");
+ like( $res->content, qr/This is template for Book/ );
+
$res = $cb->(GET "/there_is_no_such_page");
is( $res->code, 404 , '404 for non existing controller' );
$res = $cb->(GET "/ThisIsNotController/");
View
2 t/data/templates/Book/third_template
@@ -0,0 +1,2 @@
+This is template for Book.
+
View
2 t/data/templates/Product/another_template
@@ -0,0 +1,2 @@
+This is template for Product.
+
View
2 t/data/templates/Product/third_template
@@ -0,0 +1,2 @@
+This is template for Product.
+
View
19 t/lib/ControllerWithTemplates.pm
@@ -0,0 +1,19 @@
+use strict;
+use warnings;
+
+package ControllerWithTemplates;
+
+use base 'WebNano::Controller';
+use File::Spec::Functions 'catdir';
+
+my $FULLPATH;
+BEGIN { use Cwd (); $FULLPATH = Cwd::abs_path(__FILE__) }
+
+sub template_search_path {
+ my $self = shift;
+ my $mydir = $FULLPATH;
+ $mydir =~ s/.pm$//;
+ return [ catdir( $mydir, 'templates' ) ];
+}
+
+1;
View
1 t/lib/ControllerWithTemplates/templates/another_template
@@ -0,0 +1 @@
+This is the example template for ControllerWithTemplates.
View
1 t/lib/ControllerWithTemplates/templates/some_template
@@ -0,0 +1 @@
+This is the example template for ControllerWithTemplates.
View
9 t/lib/MyApp/Controller/Book.pm
@@ -0,0 +1,9 @@
+use strict;
+use warnings;
+
+package MyApp::Controller::Book;
+
+use base 'MyApp::Controller::Product';
+
+1;
+
View
18 t/lib/MyApp/Controller/Product.pm
@@ -0,0 +1,18 @@
+use strict;
+use warnings;
+
+package MyApp::Controller::Product;
+
+use base 'ControllerWithTemplates';
+
+sub template_search_path {
+ my $self = shift;
+ 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' ) }
+
+1;
+

0 comments on commit ee7d037

Please sign in to comment.