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=793ea2846ee13ef5dcb8161651f03ced3d2d4f01;hpb=83d8463c3fcb28815cc1ed74a1cb3a3797a09e9e;p=catagits%2FCatalyst-View-TT.git diff --git a/lib/Catalyst/View/TT.pm b/lib/Catalyst/View/TT.pm index 793ea28..f545ea4 100644 --- a/lib/Catalyst/View/TT.pm +++ b/lib/Catalyst/View/TT.pm @@ -10,11 +10,13 @@ use Template::Timer; use MRO::Compat; use Scalar::Util qw/blessed weaken/; -our $VERSION = '0.35'; +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; @@ -32,13 +34,10 @@ Catalyst::View::TT - Template View Class __PACKAGE__->config( # 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, + ENCODING => 'utf-8' # Not set by default PRE_PROCESS => 'config/main', WRAPPER => 'site/wrapper', @@ -46,6 +45,17 @@ Catalyst::View::TT - Template View Class expose_methods => [qw/method_in_view_class/], ); +# add include path configuration in MyApp.pm + + __PACKAGE__->config( + 'View::Web' => { + INCLUDE_PATH => [ + __PACKAGE__->path_to( 'root', 'src' ), + __PACKAGE__->path_to( 'root', 'lib' ), + ], + }, + ); + # render view from lib/MyApp.pm or lib/MyApp::Controller::SomeController.pm sub message : Global { @@ -88,6 +98,7 @@ sub new { my $config = { EVAL_PERL => 0, TEMPLATE_EXTENSION => '', + CLASS => 'Template', %{ $class->config }, %{$arguments}, }; @@ -187,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; @@ -219,7 +230,8 @@ sub process { } 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); @@ -255,7 +267,7 @@ sub render { die $self->template->error if $self->{render_die}; return $self->template->error; } - $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->debug; + $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; @@ -278,7 +290,7 @@ sub template_vars { if ($self->expose_methods) { my $meta = $self->meta; foreach my $method_name (@{$self->expose_methods}) { - my $method = $meta->get_method( $method_name ); + my $method = $meta->find_method_by_name( $method_name ); unless ($method) { Catalyst::Exception->throw( "$method_name not found in TT view" ); } @@ -316,11 +328,9 @@ replacing C with the name of your application) which looks something like this: package FooBar::View::Web; + use Moose; - use strict; - use warnings; - - use base 'Catalyst::View::TT'; + extends 'Catalyst::View::TT'; __PACKAGE__->config(DEBUG => 'all'); @@ -375,22 +385,32 @@ first way is to call the C method in the view subclass. This happens when the module is first loaded. package MyApp::View::Web; - - use strict; - use base 'Catalyst::View::TT'; + use Moose; + extends 'Catalyst::View::TT'; __PACKAGE__->config({ - INCLUDE_PATH => [ - MyApp->path_to( 'root', 'templates', 'lib' ), - MyApp->path_to( 'root', 'templates', 'src' ), - ], PRE_PROCESS => 'config/main', WRAPPER => 'site/wrapper', }); You may also override the configuration provided in the view class by adding -a 'View::Web' section to your application config (either in the application -main class, or in your configuration file). This should be reserved for +a 'View::Web' section to your application config. + +This should generally be used to inject the include paths into the view to +avoid the view trying to load the application to resolve paths. + + .. inside MyApp.pm .. + __PACKAGE__->config( + 'View::Web' => { + INCLUDE_PATH => [ + __PACKAGE__->path_to( 'root', 'templates', 'lib' ), + __PACKAGE__->path_to( 'root', 'templates', 'src' ), + ], + }, + ); + +You can also configure your view from within your config file if you're +using L. This should be reserved for deployment-specific concerns. For example: # MyApp_local.conf (Config::General format) @@ -433,6 +453,12 @@ 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 Unicode + +Be sure to set C<< ENCODING => 'utf-8' >> and use +L if you want to use non-ascii +characters (encoded as utf-8) in your templates. + =head2 RENDERING VIEWS The view plugin renders the template specified in the C