Version 0.38
[catagits/Catalyst-View-TT.git] / lib / Catalyst / View / TT.pm
index 903facb..f4d5762 100644 (file)
@@ -10,7 +10,8 @@ use Template::Timer;
 use MRO::Compat;
 use Scalar::Util qw/blessed weaken/;
 
-our $VERSION = '0.34';
+our $VERSION = '0.38';
+$VERSION = eval $VERSION;
 
 __PACKAGE__->mk_accessors('template');
 __PACKAGE__->mk_accessors('expose_methods');
@@ -32,13 +33,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 +44,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 +97,7 @@ sub new {
     my $config = {
         EVAL_PERL          => 0,
         TEMPLATE_EXTENSION => '',
+        CLASS              => 'Template',
         %{ $class->config },
         %{$arguments},
     };
@@ -187,8 +197,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;
@@ -278,7 +288,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 +326,9 @@ replacing C<MyApp> 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 +383,32 @@ first way is to call the C<config()> 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<Catalyst::Plugin::ConfigLoader>. This should be reserved for
 deployment-specific concerns. For example:
 
     # MyApp_local.conf (Config::General format)
@@ -433,6 +451,12 @@ If you are calling C<render> directly then you can specify dynamic paths by
 having a C<additional_template_paths> key with a value of additonal directories
 to search. See L<CAPTURING TEMPLATE OUTPUT> for an example showing this.
 
+=head2 Unicode
+
+Be sure to set C<< ENCODING => 'utf-8' >> and use
+L<Catalyst::Plugin::Unicode::Encoding> 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<template>
@@ -584,7 +608,7 @@ The list of methods in your View class which should be made available to the tem
 
 For example:
 
-  expose_methods => [qw/uri_for_static/],
+  expose_methods => [qw/uri_for_css/],
 
   ...
 
@@ -645,7 +669,7 @@ Would by default look for a template in <root>/test/test. If you set TEMPLATE_EX
 
 Allows you to specify the template providers that TT will use.
 
-    MyApp->config({
+    MyApp->config(
         name     => 'MyApp',
         root     => MyApp->path_to('root'),
         'View::Web' => {
@@ -662,7 +686,7 @@ Allows you to specify the template providers that TT will use.
                 }
             ]
         },
-    });
+    );
 
 The 'name' key should correspond to the class name of the provider you
 want to use.  The _file_ name is a special case that represents the default
@@ -686,6 +710,25 @@ from the general config, into the config for the provider:
 This can prove useful when you want to use the additional_template_paths hack
 in your own provider, or if you need to use Template::Provider::Encoding
 
+=head2 C<CLASS>
+
+Allows you to specify a custom class to use as the template class instead of
+L<Template>.
+
+    package MyApp::View::Web;
+    use Moose;
+    extends 'Catalyst::View::TT';
+
+    use Template::AutoFilter;
+
+    __PACKAGE__->config({
+        CLASS => 'Template::AutoFilter',
+    });
+
+This is useful if you want to use your own subclasses of L<Template>, so you
+can, for example, prevent XSS by automatically filtering all output through
+C<| html>.
+
 =head2 HELPERS
 
 The L<Catalyst::Helper::View::TT> and
@@ -728,6 +771,8 @@ Jesse Sheidlower, C<jester@panix.com>
 
 Andy Wardley, C<abw@cpan.org>
 
+Luke Saunders, C<luke.saunders@gmail.com>
+
 =head1 COPYRIGHT
 
 This program is free software. You can redistribute it and/or modify it