X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FView%2FTT.pm;h=7277ddfe476696f926708bde853fdcbeb19eb275;hb=ebba23fa2daefd50ccfbb53fcba3f68a5c5d132e;hp=8a42a16a8dd7c1af6503b336ab151939ac771258;hpb=82b741a34c944c354d03d2eacc6ac8369260d924;p=catagits%2FCatalyst-View-TT.git diff --git a/lib/Catalyst/View/TT.pm b/lib/Catalyst/View/TT.pm index 8a42a16..7277ddf 100644 --- a/lib/Catalyst/View/TT.pm +++ b/lib/Catalyst/View/TT.pm @@ -1,13 +1,15 @@ package Catalyst::View::TT; use strict; +use warnings; + use base qw/Catalyst::View/; -use Data::Dump; +use Data::Dump 'dump'; use Template; use Template::Timer; -use NEXT; +use MRO::Compat; -our $VERSION = '0.24'; +our $VERSION = '0.32'; __PACKAGE__->mk_accessors('template'); __PACKAGE__->mk_accessors('include_path'); @@ -21,47 +23,47 @@ Catalyst::View::TT - Template View Class =head1 SYNOPSIS # use the helper to create your View + myapp_create.pl view TT TT -# configure in lib/MyApp.pm +# configure in lib/MyApp.pm (Could be set from configfile instead) MyApp->config( name => 'MyApp', - root => MyApp->path_to('root');, + root => MyApp->path_to('root'), 'View::TT' => { # any TT configurations items go here INCLUDE_PATH => [ - MyApp->path_to( 'root', 'src' ), - MyApp->path_to( 'root', 'lib' ), + 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', - TEMPLATE_EXTENSION => '.tt', - - # two optional config items - CATALYST_VAR => 'Catalyst', - TIMER => 1, }, ); - -# render view from lib/MyApp.pm or lib/MyApp::C::SomeController.pm - + +# render view from lib/MyApp.pm or lib/MyApp::Controller::SomeController.pm + sub message : Global { my ( $self, $c ) = @_; $c->stash->{template} = 'message.tt2'; $c->stash->{message} = 'Hello World!'; - $c->forward('MyApp::V::TT'); + $c->forward( $c->view('TT') ); } # access variables from template The message is: [% message %]. - + # example when CATALYST_VAR is set to 'Catalyst' - Context is [% Catalyst %] - The base is [% Catalyst.req.base %] - The name is [% Catalyst.config.name %] - + Context is [% Catalyst %] + The base is [% Catalyst.req.base %] + The name is [% Catalyst.config.name %] + # example when CATALYST_VAR isn't set Context is [% c %] The base is [% base %] @@ -117,8 +119,27 @@ sub new { } if ( $c->debug && $config->{DUMP_CONFIG} ) { - $c->log->debug( "TT Config: ", Dump($config) ); + $c->log->debug( "TT Config: ", dump($config) ); } + + my $self = $class->next::method( + $c, { %$config }, + ); + + # Set base include paths. Local'd in render if needed + $self->include_path($config->{INCLUDE_PATH}); + + $self->config($config); + + # Creation of template outside of call to new so that we can pass [ $self ] + # as INCLUDE_PATH config item, which then gets ->paths() called to get list + # of include paths to search for templates. + + # Use a weakend copy of self so we dont have loops preventing GC from working + my $copy = $self; + Scalar::Util::weaken($copy); + $config->{INCLUDE_PATH} = [ sub { $copy->paths } ]; + if ( $config->{PROVIDERS} ) { my @providers = (); if ( ref($config->{PROVIDERS}) eq 'ARRAY') { @@ -131,7 +152,21 @@ sub new { } else { - $prov .="::$pname" if($pname ne '_file_'); + if($pname =~ s/^\+//) { + $prov = $pname; + } + else + { + $prov .= "::$pname"; + } + # We copy the args people want from the config + # to the args + $p->{args} ||= {}; + if ($p->{copy_config}) { + map { $p->{args}->{$_} = $config->{$_} } + grep { exists $config->{$_} } + @{ $p->{copy_config} }; + } } eval "require $prov"; if(!$@) { @@ -149,25 +184,7 @@ sub new { } } - my $self = $class->NEXT::new( - $c, { %$config }, - ); - - # Set base include paths. Local'd in render if needed - $self->include_path($config->{INCLUDE_PATH}); - - $self->config($config); - - # Creation of template outside of call to new so that we can pass [ $self ] - # as INCLUDE_PATH config item, which then gets ->paths() called to get list - # of include paths to search for templates. - - # Use a weakend copy of self so we dont have loops preventing GC from working - my $copy = $self; - Scalar::Util::weaken($copy); - $config->{INCLUDE_PATH} = [ sub { $copy->paths } ]; - - $self->{template} = + $self->{template} = Template->new($config) || do { my $error = Template->error(); $c->log->error($error); @@ -211,20 +228,20 @@ sub process { sub render { my ($self, $c, $template, $args) = @_; - $c->log->debug(qq/Rendering template "$template"/) if $c->debug; + $c->log->debug(qq/Rendering template "$template"/) if $c && $c->debug; my $output; - my $vars = { + my $vars = { (ref $args eq 'HASH' ? %$args : %{ $c->stash() }), $self->template_vars($c) }; - local $self->{include_path} = + local $self->{include_path} = [ @{ $vars->{additional_template_paths} }, @{ $self->{include_path} } ] if ref $vars->{additional_template_paths}; unless ($self->template->process( $template, $vars, \$output ) ) { - return $self->template->error; + return $self->template->error; } else { return $output; } @@ -233,6 +250,7 @@ sub render { sub template_vars { my ( $self, $c ) = @_; + return () unless $c; my $cvar = $self->config->{CATALYST_VAR}; defined $cvar @@ -260,16 +278,18 @@ of the Catalyst setup. $ script/myapp_create.pl view TT TT -This creates a MyApp::V::TT.pm module in the F directory (again, +This creates a MyApp::View::TT.pm module in the F directory (again, replacing C with the name of your application) which looks something like this: - package FooBar::V::TT; - + package FooBar::View::TT; + use strict; - use base 'Catalyst::View::TT'; + use warnings; + + use base 'Catalyst::View::TT'; - __PACKAGE__->config->{DEBUG} = 'all'; + __PACKAGE__->config(DEBUG => 'all'); Now you can modify your action handlers in the main application and/or controllers to forward to your view class. You might choose to do this @@ -277,24 +297,56 @@ in the end() method, for example, to automatically forward all actions to the TT view class. # In MyApp or MyApp::Controller::SomeController - + sub end : Private { my( $self, $c ) = @_; - $c->forward('MyApp::V::TT'); + $c->forward( $c->view('TT') ); } +But if you are using the standard auto-generated end action, you don't even need +to do this! + + # in MyApp::Controller::Root + sub end : ActionClass('RenderView') {} # no need to change this line + + # in MyApp.pm + __PACKAGE__->config( + ... + default_view => 'TT', + ); + +This will Just Work. And it has the advantages that: + +=over 4 + +=item * + +If you want to use a different view for a given request, just set +<< $c->stash->{current_view} >>. (See L's C<< $c->view >> method +for details. + +=item * + +<< $c->res->redirect >> is handled by default. If you just forward to +C in your C routine, you could break this by sending additional +content. + +=back + +See L for more details. + =head2 CONFIGURATION There are a three different ways to configure your view class. The first way is to call the C method in the view subclass. This happens when the module is first loaded. - package MyApp::V::TT; - + package MyApp::View::TT; + use strict; use base 'Catalyst::View::TT'; - MyApp::V::TT->config({ + MyApp::View::TT->config({ INCLUDE_PATH => [ MyApp->path_to( 'root', 'templates', 'lib' ), MyApp->path_to( 'root', 'templates', 'src' ), @@ -306,7 +358,7 @@ happens when the module is first loaded. The second way is to define a C method in your view subclass. This performs the configuration when the view object is created, shortly after being loaded. Remember to delegate to the base class -C method (via C<$self-ENEXT::new()> in the example below) after +C method (via C<$self-Enext::method()> in the example below) after performing any configuration. sub new { @@ -319,26 +371,26 @@ performing any configuration. PRE_PROCESS => 'config/main', WRAPPER => 'site/wrapper', }); - return $self->NEXT::new(@_); + return $self->next::method(@_); } - + The final, and perhaps most direct way, is to define a class item in your main application configuration, again by calling the -uniquitous C method. The items in the class hash are +ubiquitous C method. The items in the class hash are added to those already defined by the above two methods. This happens in the base class new() method (which is one reason why you must -remember to call it via C if you redefine the C method in a -subclass). +remember to call it via C if you redefine the C +method in a subclass). package MyApp; - + use strict; use Catalyst; - + MyApp->config({ name => 'MyApp', root => MyApp->path_to('root'), - 'V::TT' => { + 'View::TT' => { INCLUDE_PATH => [ MyApp->path_to( 'root', 'templates', 'lib' ), MyApp->path_to( 'root', 'templates', 'src' ), @@ -350,12 +402,12 @@ subclass). Note that any configuration items defined by one of the earlier methods will be overwritten by items of the same name provided by the -latter methods. +latter methods. =head2 DYNAMIC INCLUDE_PATH Sometimes it is desirable to modify INCLUDE_PATH for your templates at run time. - + Additional paths can be added to the start of INCLUDE_PATH via the stash as follows: @@ -377,24 +429,25 @@ checking and the chance of a memory leak: @{ $c->view('TT')->include_path } = qw/path another_path/; -If you are calling C directly then you can specify dynamic paths by +If you are calling C directly then you can specify dynamic paths by having a C key with a value of additonal directories to search. See L for an example showing this. =head2 RENDERING VIEWS The view plugin renders the template specified in the C