- Added additional_template_paths stash param patch from zby
Matt S Trout [Fri, 2 Dec 2005 21:14:28 +0000 (21:14 +0000)]
Makefile.PL
lib/Catalyst/View/TT.pm
t/lib/TestApp.pm

index cda8013..6a905ed 100644 (file)
@@ -6,7 +6,8 @@ WriteMakefile(
     PREREQ_PM => {
         Catalyst        => '5.5',
         Template        => 0,
-        Template::Timer => 0
+        Template::Timer => 0,
+        Path::Class     => 0,
     },
     VERSION_FROM => 'lib/Catalyst/View/TT.pm'
 );
index 6b7a828..d49cfd3 100644 (file)
@@ -9,6 +9,7 @@ use NEXT;
 our $VERSION = '0.20';
 
 __PACKAGE__->mk_accessors('template');
+__PACKAGE__->mk_accessors('include_path');
 
 =head1 NAME
 
@@ -166,6 +167,15 @@ Note that any configuration items defined by one of the earlier
 methods will be overwritten by items of the same name provided by the
 latter methods.  
 
+=head2 DYNAMIC INCLUDE_PATH
+
+It is sometimes needed to dynamically add additional paths to the 
+INCLUDE_PATH variable of the template object. This can be done by setting
+'additional_include_paths' on stash to a referrence to an array with 
+additional paths:
+
+    $c->stash->{additional_template_paths} = [$c->config->{root} . '/test_include_path']; 
+
 =head2 RENDERING VIEWS
 
 The view plugin renders the template specified in the C<template>
@@ -223,17 +233,38 @@ and reads the application config.
 
 =cut
 
-sub new {
-    my ( $class, $c, $arguments ) = @_;
+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);
+    }
+}
 
-    my $root = $c->config->{root};
 
+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 $config = {
         EVAL_PERL          => 0,
         TEMPLATE_EXTENSION => '',
-        INCLUDE_PATH       => [ $root, "$root/base" ],
         %{ $class->config },
-        %{$arguments}
+        %{$arguments},
+        INCLUDE_PATH => \@include_path,
     };
 
     # if we're debugging and/or the TIMER option is set, then we install
@@ -267,6 +298,7 @@ sub new {
         %{$config},
         },
     );
+    $self->include_path(\@include_path);
     $self->config($config);
 
     return $self;
@@ -311,7 +343,9 @@ 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;
         $error = qq/Couldn't render template "$error"/;
@@ -319,7 +353,8 @@ sub process {
         $c->error($error);
         return 0;
     }
-
+    @{$self->include_path} = @tmp_path if ref $c->stash->{additional_template_paths};
+   
     unless ( $c->response->content_type ) {
         $c->response->content_type('text/html; charset=utf-8');
     }
index 12d9bc0..618a5fd 100755 (executable)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 
 use Catalyst; # qw/-Debug/;
+use Path::Class;
 
 our $VERSION = '0.01';
 
@@ -32,6 +33,16 @@ sub test : Local {
     $c->stash->{message} = ($c->request->param('message') || $c->config->{default_message});
 }
 
+sub test_includepath : Local {
+    my ($self, $c) = @_;
+    $c->stash->{message} = ($c->request->param('message') || $c->config->{default_message});
+    $c->stash->{template} = $c->request->param('template');
+    if ( $c->request->param('additionalpath') ){
+        my $additionalpath = Path::Class::dir($c->config->{root}, $c->request->param('additionalpath'));
+        $c->stash->{additional_template_paths} = ["$additionalpath"];
+    }
+}
+
 sub end : Private {
     my ($self, $c) = @_;