From: Marcus Ramberg Date: Wed, 14 Dec 2005 23:23:49 +0000 (+0000) Subject: fix dynamic include bugs. X-Git-Tag: v0.30~44 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3a4ffa2aa8ec654b70446b54043eb49bac55079e;hp=63dee50bc80b0bc1d05afa628f25b17599c92f2a;p=catagits%2FCatalyst-View-TT.git fix dynamic include bugs. --- diff --git a/Changes b/Changes index b8b39f9..e4d4cbf 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for Perl extension Catalyst::View::TT. +0.21 + - Fixed dynamic include bugs and added tests. 0.20 - Fall back to action (default/index returns blank on match) - Added tests. (Daniel Westermann-Clark) diff --git a/lib/Catalyst/View/TT.pm b/lib/Catalyst/View/TT.pm index 26b5079..2b6ddb7 100644 --- a/lib/Catalyst/View/TT.pm +++ b/lib/Catalyst/View/TT.pm @@ -237,34 +237,40 @@ sub _coerce_paths { my ( $paths, $dlim ) = shift; return () if ( ! $paths ); return @{$paths} if ( ref $paths eq 'ARRAY'); - if ( ! ref $paths ){ # tweak delim to ignore C:/ unless (defined $dlim) { $dlim = ($^O eq 'MSWin32') ? ':(?!\\/)' : ':'; } return split(/$dlim/, $paths); - } } sub new { my ( $class, $c, $arguments ) = @_; my $delim = $class->config->{DELIMITER} || $arguments->{DELIMITER}; - my @include_path = _coerce_paths($arguments->{INCLUDE_PATH}, $delim); - if(!@include_path){ - @include_path = _coerce_paths($class->config->{INCLUDE_PATH}, $delim); - } - if(!@include_path){ - my $root = $c->config->{root}; - my $base = Path::Class::dir($root, 'base'); - @include_path = ( "$root", "$base" ); + my $include_path; + if(ref $arguments->{INCLUDE_PATH} eq 'ARRAY'){ + $include_path = $arguments->{INCLUDE_PATH}; + }elsif(ref $class->config->{INCLUDE_PATH} eq 'ARRAY'){ + $include_path = $class->config->{INCLUDE_PATH}; + }else{ + my @include_path = _coerce_paths($arguments->{INCLUDE_PATH}, $delim); + if(!@include_path){ + @include_path = _coerce_paths($class->config->{INCLUDE_PATH}, $delim); + } + if(!@include_path){ + my $root = $c->config->{root}; + my $base = Path::Class::dir($root, 'base'); + @include_path = ( "$root", "$base" ); + } + $include_path = \@include_path; } my $config = { EVAL_PERL => 0, TEMPLATE_EXTENSION => '', %{ $class->config }, %{$arguments}, - INCLUDE_PATH => \@include_path, + INCLUDE_PATH => $include_path, }; # if we're debugging and/or the TIMER option is set, then we install @@ -298,7 +304,7 @@ sub new { %{$config}, }, ); - $self->include_path(\@include_path); + $self->include_path($include_path); $self->config($config); return $self; @@ -344,8 +350,6 @@ sub process { ), %{ $c->stash() } }; - - my @tmp_path = @{$self->include_path}; unshift @{$self->include_path}, @{$c->stash->{additional_template_paths}} if ref $c->stash->{additional_template_paths}; unless ( $self->template->process( $template, $vars, \$output ) ) { my $error = $self->template->error; @@ -354,7 +358,7 @@ sub process { $c->error($error); return 0; } - @{$self->include_path} = @tmp_path if ref $c->stash->{additional_template_paths}; + splice @{$self->include_path}, 0, scalar @{$c->stash->{additional_template_paths}} if ref $c->stash->{additional_template_paths}; unless ( $c->response->content_type ) { $c->response->content_type('text/html; charset=utf-8'); diff --git a/t/06includepath.t b/t/06includepath.t index f6b5304..0da5a8f 100644 --- a/t/06includepath.t +++ b/t/06includepath.t @@ -1,6 +1,6 @@ use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 9; use FindBin; use lib "$FindBin::Bin/lib"; @@ -8,10 +8,15 @@ use lib "$FindBin::Bin/lib"; use_ok('Catalyst::Test', 'TestApp'); my $response; -ok(($response = request("/test_includepath?view=Appconfig&template=testpath.tt&additionalpath=test_include_path"))->is_success, 'request ok'); +ok(($response = request("/test_includepath?view=Appconfig&template=testpath.tt&additionalpath=test_include_path"))->is_success, 'additional_template_path'); is($response->content, TestApp->config->{default_message}, 'message ok'); +ok(($response = request("/test_includepath?view=Includepath&template=testpath.tt"))->is_success, 'scalar include path from config'); +is($response->content, TestApp->config->{default_message}, 'message ok'); + +ok(($response = request("/test_includepath?view=Includepath2&template=testpath.tt"))->is_success, 'object ref (that stringifys to the path) include path from config'); +is($response->content, TestApp->config->{default_message}, 'message ok'); -ok(($response = request("/test_includepath?view=Includepath&template=testpath.tt"))->is_success, 'request ok'); +ok(($response = request("/test_includepath?view=Includepath3&template=testpath.tt&addpath=test_include_path"))->is_success, 'array ref include path from config not replaced by another array'); is($response->content, TestApp->config->{default_message}, 'message ok'); diff --git a/t/lib/TestApp.pm b/t/lib/TestApp.pm index 618a5fd..5220859 100755 --- a/t/lib/TestApp.pm +++ b/t/lib/TestApp.pm @@ -41,6 +41,13 @@ sub test_includepath : Local { my $additionalpath = Path::Class::dir($c->config->{root}, $c->request->param('additionalpath')); $c->stash->{additional_template_paths} = ["$additionalpath"]; } + if ( $c->request->param('addpath') ){ + my $additionalpath = Path::Class::dir($c->config->{root}, $c->request->param('addpath')); + my $view = 'TestApp::View::TT::' . ($c->request->param('view') || $c->config->{default_view}); + no strict "refs"; + push @{$view . '::include_path'}, "$additionalpath"; + use strict; + } } sub end : Private {