X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FView%2FTT.pm;h=6b7a828f2214e2c1124e78b6a0146354a47c57c5;hb=07571b2fe7833e9db29e8e0cafb633b9fb5edc52;hp=4e948697a8304163cd8efef40586c663c3f212f5;hpb=19ee577af75c8bdd9b41bf972bd76a69b6a822ea;p=catagits%2FCatalyst-View-TT.git diff --git a/lib/Catalyst/View/TT.pm b/lib/Catalyst/View/TT.pm index 4e94869..6b7a828 100644 --- a/lib/Catalyst/View/TT.pm +++ b/lib/Catalyst/View/TT.pm @@ -1,12 +1,12 @@ package Catalyst::View::TT; use strict; -use base qw/Catalyst::Base/; +use base qw/Catalyst::View/; use Template; use Template::Timer; use NEXT; -our $VERSION = '0.14'; +our $VERSION = '0.20'; __PACKAGE__->mk_accessors('template'); @@ -21,19 +21,18 @@ Catalyst::View::TT - Template View Class # configure in lib/MyApp.pm - our $ROOT = '/home/dent/catalyst/MyApp'; - MyApp->config({ name => 'MyApp', - root => $ROOT, - 'MyApp::V::TT' => { + root => MyApp->path_to('root');, + 'V::TT' => { # any TT configurations items go here INCLUDE_PATH => [ - "$ROOT/templates/src", - "$ROOT/templates/lib" + MyApp->path_to( 'root', 'src' ), + MyApp->path_to( 'root', 'lib' ), ], - PRE_PROCESS => 'config/main', - WRAPPER => 'site/wrapper', + PRE_PROCESS => 'config/main', + WRAPPER => 'site/wrapper', + TEMPLATE_EXTENSION => '.tt', # two optional config items CATALYST_VAR => 'Catalyst', @@ -109,10 +108,11 @@ happens when the module is first loaded. use strict; use base 'Catalyst::View::TT'; - our $ROOT = '/home/dent/catalyst/MyApp'; - MyApp::V::TT->config({ - INCLUDE_PATH => ["$ROOT/templates/src", "$ROOT/templates/lib"], + INCLUDE_PATH => [ + MyApp->path_to( 'root', 'templates', 'lib' ), + MyApp->path_to( 'root', 'templates', 'src' ), + ], PRE_PROCESS => 'config/main', WRAPPER => 'site/wrapper', }); @@ -126,7 +126,10 @@ performing any configuration. sub new { my $self = shift; $self->config({ - INCLUDE_PATH => ["$ROOT/templates/src", "$ROOT/templates/lib"], + INCLUDE_PATH => [ + MyApp->path_to( 'root', 'templates', 'lib' ), + MyApp->path_to( 'root', 'templates', 'src' ), + ], PRE_PROCESS => 'config/main', WRAPPER => 'site/wrapper', }); @@ -146,13 +149,14 @@ subclass). use strict; use Catalyst; - our $ROOT = '/home/dent/catalyst/MyApp'; - MyApp->config({ name => 'MyApp', - root => $ROOT, - 'MyApp::V::TT' => { - INCLUDE_PATH => ["$ROOT/templates/src", "$ROOT/templates/lib"], + root => MyApp->path_to('root'), + 'V::TT' => { + INCLUDE_PATH => [ + MyApp->path_to( 'root', 'templates', 'lib' ), + MyApp->path_to( 'root', 'templates', 'src' ), + ], PRE_PROCESS => 'config/main', WRAPPER => 'site/wrapper', }, @@ -202,52 +206,12 @@ These can be accessed from the template in the usual way: The base is [% base %] The name is [% name %] -If you prefer, you can set the C configuration item to -define the name of a template variable through which the context can -be referenced. - - MyApp->config({ - name => 'MyApp', - root => $ROOT, - 'MyApp::V::TT' => { - CATALYST_VAR => 'Catalyst', - }, - }); - -F: - - The base is [% Catalyst.req.base %] - The name is [% Catalyst.config.name %] The output generated by the template is stored in C<$c-Eresponse-Eoutput>. =head2 TEMPLATE PROFILING -If you have configured Catalyst for debug output, -C will enable profiling of template processing -(using L). This will embed HTML comments in the -output from your templates, such as: - - - - - - - - .... - - - -You can suppress template profiling by setting the C configuration -item to a false value. - - MyApp->config({ - 'MyApp::V::TT' => { - TIMER => 0, - }, - }); - =head2 METHODS =over 4 @@ -265,8 +229,9 @@ sub new { my $root = $c->config->{root}; my $config = { - EVAL_PERL => 0, - INCLUDE_PATH => [ $root, "$root/base" ], + EVAL_PERL => 0, + TEMPLATE_EXTENSION => '', + INCLUDE_PATH => [ $root, "$root/base" ], %{ $class->config }, %{$arguments} }; @@ -275,7 +240,7 @@ sub new { # Template::Timer as a custom CONTEXT object, but only if we haven't # already got a custom CONTEXT defined - if ( $config->{TIMER} || ( $c->debug() && !exists $config->{TIMER} ) ) { + if ( $config->{TIMER} ) { if ( $config->{CONTEXT} ) { $c->log->error( 'Cannot use Template::Timer - a TT CONFIG is already defined'); @@ -290,7 +255,7 @@ sub new { $c->log->debug( "TT Config: ", Dumper($config) ); } - return $class->NEXT::new( + my $self = $class->NEXT::new( $c, { template => Template->new($config) || do { @@ -298,9 +263,13 @@ sub new { $c->log->error($error); $c->error($error); return undef; - } - } + }, + %{$config}, + }, ); + $self->config($config); + + return $self; } =item process @@ -320,7 +289,8 @@ Output is stored in C<$c-Eresponse-Eoutput>. sub process { my ( $self, $c ) = @_; - my $template = $c->stash->{template} || $c->request->match; + my $template = $c->stash->{template} + || $c->request->match . $self->config->{TEMPLATE_EXTENSION}; unless ($template) { $c->log->debug('No template specified for rendering') if $c->debug; @@ -362,8 +332,61 @@ sub process { =item config This method allows your view subclass to pass additional settings to -the TT configuration hash, or to set the C and C -options. +the TT configuration hash, or to set the options as below: + +=over 2 + +=item C + +Allows you to change the name of the Catalyst context object. If set, it will also +remove the base and name aliases, so you will have access them through . + +For example: + + MyApp->config({ + name => 'MyApp', + root => MyApp->path_to('root'), + 'V::TT' => { + CATALYST_VAR => 'Catalyst', + }, + }); + +F: + + The base is [% Catalyst.req.base %] + The name is [% Catalyst.config.name %] + +=item C + +If you have configured Catalyst for debug output, and turned on the TIMER setting, +C will enable profiling of template processing +(using L). This will embed HTML comments in the +output from your templates, such as: + + + + + + + + .... + + + + +=item C + +a sufix to add when looking for templates bases on the C method in L. + +For example: + + package MyApp::C::Test; + sub test : Local { .. } + +Would by default look for a template in /test/test. If you set TEMPLATE_EXTENSION to '.tt', it will look for +/test/test.tt. + +=back =back