More concise render_die message
[catagits/Catalyst-View-TT.git] / lib / Catalyst / View / TT.pm
index 0426b93..4084fe2 100644 (file)
@@ -8,8 +8,9 @@ use Data::Dump 'dump';
 use Template;
 use Template::Timer;
 use MRO::Compat;
+use Scalar::Util qw/blessed/;
 
-our $VERSION = '0.32';
+our $VERSION = '0.34';
 
 __PACKAGE__->mk_accessors('template');
 __PACKAGE__->mk_accessors('include_path');
@@ -28,9 +29,10 @@ Catalyst::View::TT - Template View Class
 
 # configure in lib/MyApp.pm (Could be set from configfile instead)
 
-    MyApp->config(
-        name     => 'MyApp',
-        root     => MyApp->path_to('root'),
+    __PACKAGE__->config(
+        name         => 'MyApp',
+        root         => MyApp->path_to('root'),
+        default_view => 'TT',
         'View::TT' => {
             # any TT configurations items go here
             INCLUDE_PATH => [
@@ -43,6 +45,7 @@ Catalyst::View::TT - Template View Class
             # Not set by default
             PRE_PROCESS        => 'config/main',
             WRAPPER            => 'site/wrapper',
+            render_die => 1, # Default for new apps, see render method docs
         },
     );
 
@@ -211,10 +214,10 @@ sub process {
     local $@;
     my $output = eval { $self->render($c, $template) };
     if (my $err = $@) {
-        my $error = qq/Couldn't render template "$template"/;
-        $c->log->error($error);
-        $c->error($error);
-        return 0;
+        return $self->_rendering_error($c, $err);
+    }
+    if (blessed($output) && $output->isa('Template::Exception')) {
+        $self->_rendering_error($c, $output);
     }
 
     unless ( $c->response->content_type ) {
@@ -226,6 +229,14 @@ sub process {
     return 1;
 }
 
+sub _rendering_error {
+    my ($self, $c, $err) = @_;
+    my $error = qq/Couldn't render template "$err"/;
+    $c->log->error($error);
+    $c->error($error);
+    return 0;
+}
+
 sub render {
     my ($self, $c, $template, $args) = @_;
 
@@ -241,8 +252,14 @@ sub render {
         [ @{ $vars->{additional_template_paths} }, @{ $self->{include_path} } ]
         if ref $vars->{additional_template_paths};
 
-    $self->template->process( $template, $vars, \$output )
-        or die $self->template->error;
+    unless ( $self->template->process( $template, $vars, \$output ) ) {
+        if (exists $self->{render_die}) {
+            die $self->template->error if $self->{render_die};
+            return $self->template->error;
+        }
+        $c->log->debug('The Catalyst::View::TT render() method will start dying on error in a future release. Unless you are calling the render() method manually, you probably want the new behaviour, so set render_die => 1 in config for ' . blessed($self) . '. If you wish to continue to return the exception rather than throwing it, add render_die => 0 to your config.') if $c->debug;
+        return $self->template->error;
+    }
     return $output;
 }
 
@@ -547,6 +564,22 @@ to render page fragments like this:
 
     my $fragment = $c->forward("View::TT", "render", $template_name, $c->stash->{fragment_data});
 
+=head3 Backwards compatibility note
+
+The render method used to just return the Template::Exception object, rather
+than just throwing it. This is now deprecated and instead the render method
+will throw an exception for new applications.
+
+This behaviour can be activated (and is activated in the default skeleton
+configuration) by using C<< render_die => 1 >>. If you rely on the legacy
+behaviour then a warning will be issued.
+
+To silence this warning, set C<< render_die => 0 >>, but it is recommended
+you adjust your code so that it works with C<< render_die => 1 >>.
+
+In a future release, C<< render_die => 1 >> will become the default if
+unspecified.
+
 =head2 template_vars
 
 Returns a list of keys/values to be used as the catalyst variables in the