From: Tomas Doran Date: Wed, 10 Mar 2010 20:05:23 +0000 (+0000) Subject: r11755@t0mlaptop (orig r11720): theory | 2009-11-03 00:23:43 +0000 X-Git-Tag: v0.33^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Ftags%2Fv0.33;hp=ebba23fa2daefd50ccfbb53fcba3f68a5c5d132e;p=catagits%2FCatalyst-View-TT.git r11755@t0mlaptop (orig r11720): theory | 2009-11-03 00:23:43 +0000 Branch Catalyst::View::TT to make `render()` die instead of return an exception object. r11756@t0mlaptop (orig r11721): theory | 2009-11-03 00:37:04 +0000 Have `render()` die on exception, not return an exception object. r11832@t0mlaptop (orig r11797): osfameron | 2009-11-11 10:58:24 +0000 Clarify docs for 'sub end' to promote RenderView (Previously, the docs suggested doing something that would break $c->res->redirect) r11834@t0mlaptop (orig r11799): t0m | 2009-11-12 00:31:26 +0000 Back out 11797 - misscommit to wrong branch. svn merge -r11797:11796 . r12947@t0mlaptop (orig r12911): t0m | 2010-02-16 22:37:01 +0000 Localise execeptions r13074@t0mlaptop (orig r13038): theory | 2010-03-10 18:24:49 +0000 Have render() warn on exception, rather than die. This is at mst's request, to minimize backward compatibility issues. To silence the warning, pass `render_die => 0` to the constructor. Better yet, pass 'render_die => 1' to make it die instead of returning the excption. This will be the default in a future release. r13075@t0mlaptop (orig r13039): t0m | 2010-03-10 20:02:47 +0000 Document setting, make skeleton generate it by default r13076@t0mlaptop (orig r13040): t0m | 2010-03-10 20:04:57 +0000 Bump versions --- diff --git a/Changes b/Changes index 845c2f9..2f6c7ad 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,13 @@ Revision history for Perl extension Catalyst::View::TT. +0.33 2010-03-10 20:08:00 + - The "render()" method now throws a warning on exception before + returning the exception. To silence the warning, pass 'render_die => + 0' to the constructor. Better yet, pass 'render_die => 1' to make it + die instead of returning the excption. This will be the default in a + future release when unspecified. The Helper will generate new views + with render_die => 1. + 0.32 2010-02-16 05:55:00 - Various documentation improvements. - Fix repository metadata. diff --git a/lib/Catalyst/Helper/View/TT.pm b/lib/Catalyst/Helper/View/TT.pm index 6c9462f..9dc6155 100644 --- a/lib/Catalyst/Helper/View/TT.pm +++ b/lib/Catalyst/Helper/View/TT.pm @@ -55,7 +55,10 @@ use warnings; use base 'Catalyst::View::TT'; -__PACKAGE__->config(TEMPLATE_EXTENSION => '.tt'); +__PACKAGE__->config( + TEMPLATE_EXTENSION => '.tt', + render_die => 1, +); =head1 NAME diff --git a/lib/Catalyst/View/TT.pm b/lib/Catalyst/View/TT.pm index 7277ddf..4c0662b 100644 --- a/lib/Catalyst/View/TT.pm +++ b/lib/Catalyst/View/TT.pm @@ -9,7 +9,7 @@ use Template; use Template::Timer; use MRO::Compat; -our $VERSION = '0.32'; +our $VERSION = '0.33'; __PACKAGE__->mk_accessors('template'); __PACKAGE__->mk_accessors('include_path'); @@ -43,6 +43,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 }, ); @@ -168,6 +169,7 @@ sub new { @{ $p->{copy_config} }; } } + local $@; eval "require $prov"; if(!$@) { push @providers, "$prov"->new($p->{args}); @@ -207,10 +209,10 @@ sub process { return 0; } - my $output = $self->render($c, $template); - - if (UNIVERSAL::isa($output, 'Template::Exception')) { - my $error = qq/Couldn't render template "$output"/; + 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; @@ -240,11 +242,16 @@ sub render { [ @{ $vars->{additional_template_paths} }, @{ $self->{include_path} } ] if ref $vars->{additional_template_paths}; - unless ($self->template->process( $template, $vars, \$output ) ) { + unless ( $self->template->process( $template, $vars, \$output ) ) { + if (exists $self->{render_die}) { + die $self->template->error if $self->{render_die}; + return $self->template->error; + } + require Carp; + Carp::carp('The Catalyst::View::TT render() method of will die on error in a future release. If you want it to continue to return the exception instead, pass render_die => 0 to the constructor'); return $self->template->error; - } else { - return $output; } + return $output; } sub template_vars { @@ -525,7 +532,7 @@ N.B. This is usually done automatically by L. =head2 render($c, $template, \%args) -Renders the given template and returns output, or a L +Renders the given template and returns output. Throws a L object upon error. The template variables are set to C<%$args> if $args is a hashref, or @@ -548,6 +555,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 diff --git a/t/lib/TestApp.pm b/t/lib/TestApp.pm index 97bf8f0..399f813 100755 --- a/t/lib/TestApp.pm +++ b/t/lib/TestApp.pm @@ -16,6 +16,7 @@ __PACKAGE__->config( PRE_CHOMP => 1, POST_CHOMP => 1, TEMPLATE_EXTENSION => '.tt', + render_die => 1, }, ); diff --git a/t/lib/TestApp/Controller/Root.pm b/t/lib/TestApp/Controller/Root.pm index e119a1b..41a0d46 100644 --- a/t/lib/TestApp/Controller/Root.pm +++ b/t/lib/TestApp/Controller/Root.pm @@ -34,9 +34,9 @@ sub test_includepath : Local { sub test_render : Local { my ($self, $c) = @_; - my $out = $c->stash->{message} = $c->view('TT::Appconfig')->render($c, $c->req->param('template'), {param => $c->req->param('param') || ''}); - if (UNIVERSAL::isa($out, 'Template::Exception')) { - $c->response->body($out); + $c->stash->{message} = eval { $c->view('TT::Appconfig')->render($c, $c->req->param('template'), {param => $c->req->param('param') || ''}) }; + if (my $err = $@) { + $c->response->body($err); $c->response->status(403); } else { $c->stash->{template} = 'test.tt';