X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FView%2FTT.pm;h=f545ea434ff6fc397c655130e79d13ed508c6d84;hb=ee06e29a8475f5dd926aa81c6fcce4f75b7d7585;hp=80ab8546be31363175cd9a3c59892f6c664dc788;hpb=940a2e8aca13f60eb214dabbc8152a8f1268437d;p=catagits%2FCatalyst-View-TT.git diff --git a/lib/Catalyst/View/TT.pm b/lib/Catalyst/View/TT.pm index 80ab854..f545ea4 100644 --- a/lib/Catalyst/View/TT.pm +++ b/lib/Catalyst/View/TT.pm @@ -8,11 +8,15 @@ use Data::Dump 'dump'; use Template; use Template::Timer; use MRO::Compat; +use Scalar::Util qw/blessed weaken/; -our $VERSION = '0.33'; +our $VERSION = '0.41'; +$VERSION = eval $VERSION; __PACKAGE__->mk_accessors('template'); +__PACKAGE__->mk_accessors('expose_methods'); __PACKAGE__->mk_accessors('include_path'); +__PACKAGE__->mk_accessors('content_type'); *paths = \&include_path; @@ -24,27 +28,31 @@ Catalyst::View::TT - Template View Class # use the helper to create your View - myapp_create.pl view TT TT + myapp_create.pl view Web TT -# configure in lib/MyApp.pm (Could be set from configfile instead) +# add custom configration in View/Web.pm __PACKAGE__->config( - name => 'MyApp', - root => MyApp->path_to('root'), - default_view => 'TT', - 'View::TT' => { - # any TT configurations items go here + # any TT configuration items go here + TEMPLATE_EXTENSION => '.tt', + CATALYST_VAR => 'c', + TIMER => 0, + ENCODING => 'utf-8' + # Not set by default + PRE_PROCESS => 'config/main', + WRAPPER => 'site/wrapper', + render_die => 1, # Default for new apps, see render method docs + expose_methods => [qw/method_in_view_class/], + ); + +# add include path configuration in MyApp.pm + + __PACKAGE__->config( + 'View::Web' => { INCLUDE_PATH => [ - MyApp->path_to( 'root', 'src' ), - MyApp->path_to( 'root', 'lib' ), + __PACKAGE__->path_to( 'root', 'src' ), + __PACKAGE__->path_to( 'root', 'lib' ), ], - TEMPLATE_EXTENSION => '.tt', - CATALYST_VAR => 'c', - TIMER => 0, - # Not set by default - PRE_PROCESS => 'config/main', - WRAPPER => 'site/wrapper', - render_die => 1, # Default for new apps, see render method docs }, ); @@ -54,7 +62,7 @@ Catalyst::View::TT - Template View Class my ( $self, $c ) = @_; $c->stash->{template} = 'message.tt2'; $c->stash->{message} = 'Hello World!'; - $c->forward( $c->view('TT') ); + $c->forward( $c->view('Web') ); } # access variables from template @@ -90,6 +98,7 @@ sub new { my $config = { EVAL_PERL => 0, TEMPLATE_EXTENSION => '', + CLASS => 'Template', %{ $class->config }, %{$arguments}, }; @@ -131,6 +140,7 @@ sub new { # Set base include paths. Local'd in render if needed $self->include_path($config->{INCLUDE_PATH}); + $self->expose_methods($config->{expose_methods}); $self->config($config); # Creation of template outside of call to new so that we can pass [ $self ] @@ -188,8 +198,8 @@ sub new { } $self->{template} = - Template->new($config) || do { - my $error = Template->error(); + $config->{CLASS}->new($config) || do { + my $error = $config->{CLASS}->error(); $c->log->error($error); $c->error($error); return undef; @@ -213,14 +223,15 @@ 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, $template . ': ' . $err); + } + if (blessed($output) && $output->isa('Template::Exception')) { + $self->_rendering_error($c, $output); } unless ( $c->response->content_type ) { - $c->response->content_type('text/html; charset=utf-8'); + my $default = $self->content_type || 'text/html; charset=utf-8'; + $c->response->content_type($default); } $c->response->body($output); @@ -228,6 +239,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) = @_; @@ -248,8 +267,7 @@ sub render { 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'); + $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 && $c->debug; return $self->template->error; } return $output; @@ -261,16 +279,33 @@ sub template_vars { return () unless $c; my $cvar = $self->config->{CATALYST_VAR}; - defined $cvar + my %vars = defined $cvar ? ( $cvar => $c ) : ( c => $c, base => $c->req->base, name => $c->config->{name} - ) + ); + + if ($self->expose_methods) { + my $meta = $self->meta; + foreach my $method_name (@{$self->expose_methods}) { + my $method = $meta->find_method_by_name( $method_name ); + unless ($method) { + Catalyst::Exception->throw( "$method_name not found in TT view" ); + } + my $method_body = $method->body; + my $weak_ctx = $c; + weaken $weak_ctx; + my $sub = sub { + $self->$method_body($weak_ctx, @_); + }; + $vars{$method_name} = $sub; + } + } + return %vars; } - 1; __END__ @@ -279,23 +314,23 @@ __END__ This is the Catalyst view class for the L