woops, backfix, re-added template_vars changes
[catagits/Catalyst-View-TT.git] / lib / Catalyst / View / TT.pm
index d1353e4..74d538d 100644 (file)
@@ -247,35 +247,25 @@ sub _coerce_paths {
 
 sub new {
     my ( $class, $c, $arguments ) = @_;
-    my $delim = $class->config->{DELIMITER} || $arguments->{DELIMITER};
-    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 $config = {
+        EVAL_PERL          => 0,
+        TEMPLATE_EXTENSION => '',
+        %{ $class->config },
+        %{$arguments},
+    };
+    if ( ! (ref $config->{INCLUDE_PATH} eq 'ARRAY') ) {
+        my $delim = $config->{DELIMITER};
         my @include_path
-            = _coerce_paths( $arguments->{INCLUDE_PATH}, $delim );
-        if ( !@include_path ) {
-            @include_path
-                = _coerce_paths( $class->config->{INCLUDE_PATH}, $delim );
-        }
+            = _coerce_paths( $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;
+        $config->{INCLUDE_PATH} = \@include_path;
     }
-    my $config = {
-        EVAL_PERL          => 0,
-        TEMPLATE_EXTENSION => '',
-        %{ $class->config },
-        %{$arguments},
-        INCLUDE_PATH => $include_path,
-    };
+
+
 
     # if we're debugging and/or the TIMER option is set, then we install
     # Template::Timer as a custom CONTEXT object, but only if we haven't
@@ -296,6 +286,22 @@ sub new {
         use Data::Dumper;
         $c->log->debug( "TT Config: ", Dumper($config) );
     }
+    if ( $config->{PROVIDERS} ) {
+        my @providers = ();
+        if ( ref($config->{PROVIDERS}) eq 'ARRAY') {
+            foreach my $p (@{$config->{PROVIDERS}}) {
+                my $pname = $p->{name};
+                eval "require Template::Provider::$pname";
+                if(!$@) {
+                    push @providers, "Template::Provider::${pname}"->new($p->{args});
+                }
+            }
+        }
+        delete $config->{PROVIDERS};
+        if(@providers) {
+            $config->{LOAD_TEMPLATES} = \@providers;
+        }
+    }
 
     my $self = $class->NEXT::new(
         $c,
@@ -308,7 +314,7 @@ sub new {
             %{$config},
         },
     );
-    $self->include_path($include_path);
+    $self->include_path($config->{INCLUDE_PATH});
     $self->config($config);
 
     return $self;
@@ -332,8 +338,8 @@ sub process {
     my ( $self, $c ) = @_;
 
     my $template = $c->stash->{template}
-        || ( $c->request->match || $c->request->action )
-        . $self->config->{TEMPLATE_EXTENSION};
+      || ( $c->request->match || $c->request->action )
+      . $self->config->{TEMPLATE_EXTENSION};
 
     unless ($template) {
         $c->log->debug('No template specified for rendering') if $c->debug;
@@ -343,19 +349,11 @@ sub process {
     $c->log->debug(qq/Rendering template "$template"/) if $c->debug;
 
     my $output;
-    my $cvar = $self->config->{CATALYST_VAR};
-    my $vars = {
-        defined $cvar
-        ? ( $cvar => $c )
-        : ( c    => $c,
-            base => $c->req->base,
-            name => $c->config->{name}
-        ),
-        %{ $c->stash() }
-    };
+    my $vars = { $self->template_vars($c) };
+
     unshift @{ $self->include_path },
-        @{ $c->stash->{additional_template_paths} }
-        if ref $c->stash->{additional_template_paths};
+      @{ $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"/;
@@ -364,8 +362,8 @@ sub process {
         return 0;
     }
     splice @{ $self->include_path }, 0,
-        scalar @{ $c->stash->{additional_template_paths} }
-        if ref $c->stash->{additional_template_paths};
+      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');
@@ -376,6 +374,28 @@ sub process {
     return 1;
 }
 
+=item template_vars
+
+Returns a list of keys/values to be used as the variables in the
+template.
+
+=cut
+
+sub template_vars {
+    my ( $self, $c ) = @_;
+
+    my $cvar = $self->config->{CATALYST_VAR};
+
+    defined $cvar
+      ? ( $cvar => $c )
+      : (
+        c    => $c,
+        base => $c->req->base,
+        name => $c->config->{name}
+      ),
+      %{ $c->stash() }
+
+}
 =item config
 
 This method allows your view subclass to pass additional settings to