X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FView%2FTT.pm;h=f51331feb31d63673028b778a8e52f9dc0ef7d31;hb=7bd88b6108e3a78197a7a6a436e4e3466eb38571;hp=964411dc70862fcbf38756a47eebc03976cda67e;hpb=3d543eda0c6f0e5ede10c93f265f9678bd31b0d5;p=catagits%2FCatalyst-View-TT.git diff --git a/lib/Catalyst/View/TT.pm b/lib/Catalyst/View/TT.pm index 964411d..f51331f 100644 --- a/lib/Catalyst/View/TT.pm +++ b/lib/Catalyst/View/TT.pm @@ -8,11 +8,13 @@ use Data::Dump 'dump'; use Template; use Template::Timer; use MRO::Compat; -use Scalar::Util qw/blessed/; +use Scalar::Util qw/blessed weaken/; -our $VERSION = '0.34'; +our $VERSION = '0.36'; +$VERSION = eval $VERSION; __PACKAGE__->mk_accessors('template'); +__PACKAGE__->mk_accessors('expose_methods'); __PACKAGE__->mk_accessors('include_path'); *paths = \&include_path; @@ -25,28 +27,24 @@ 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 - INCLUDE_PATH => [ - MyApp->path_to( 'root', 'src' ), - MyApp->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 - }, + # any TT configuration items go here + INCLUDE_PATH => [ + MyApp->path_to( 'root', 'src' ), + MyApp->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 + expose_methods => [qw/method_in_view_class/], ); # render view from lib/MyApp.pm or lib/MyApp::Controller::SomeController.pm @@ -55,7 +53,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 @@ -91,6 +89,7 @@ sub new { my $config = { EVAL_PERL => 0, TEMPLATE_EXTENSION => '', + CLASS => 'Template', %{ $class->config }, %{$arguments}, }; @@ -132,6 +131,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 ] @@ -189,8 +189,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; @@ -214,7 +214,7 @@ sub process { local $@; my $output = eval { $self->render($c, $template) }; if (my $err = $@) { - return $self->_rendering_error($c, $err); + return $self->_rendering_error($c, $template . ': ' . $err); } if (blessed($output) && $output->isa('Template::Exception')) { $self->_rendering_error($c, $output); @@ -269,16 +269,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__ @@ -287,18 +304,20 @@ __END__ This is the Catalyst view class for the L