the class used to create the TT object can now be customized feature/customize_tt_class mirror/feature/customize_tt_class
Christian Walde [Sat, 16 Jul 2011 18:53:35 +0000 (20:53 +0200)]
lib/Catalyst/View/TT.pm
t/13classconfig.t [new file with mode: 0644]
t/lib/TestApp/Template/Any.pm [new file with mode: 0644]
t/lib/TestApp/View/TT/Classconfig.pm [new file with mode: 0644]
t/lib/TestApp/root/any_include_path/test [new file with mode: 0644]

index 9dbb34c..f51331f 100644 (file)
@@ -89,6 +89,7 @@ sub new {
     my $config = {
         EVAL_PERL          => 0,
         TEMPLATE_EXTENSION => '',
+        CLASS              => 'Template',
         %{ $class->config },
         %{$arguments},
     };
@@ -188,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;
@@ -687,6 +688,26 @@ 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 strict;
+    use base '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
diff --git a/t/13classconfig.t b/t/13classconfig.t
new file mode 100644 (file)
index 0000000..20b27c6
--- /dev/null
@@ -0,0 +1,18 @@
+use strict;
+use warnings;
+use Test::More tests => 5;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use_ok('Catalyst::Test', 'TestApp');
+
+my $view = 'Classconfig';
+
+my $response;
+ok(($response = request("/test?view=$view"))->is_success, 'request ok');
+is($response->content, TestApp->config->{default_message}, 'message ok');
+
+my $message = scalar localtime;
+ok(($response = request("/test?view=$view&message=$message"))->is_success, 'request with message ok');
+is($response->content,  $message, 'message ok')
diff --git a/t/lib/TestApp/Template/Any.pm b/t/lib/TestApp/Template/Any.pm
new file mode 100644 (file)
index 0000000..44594c9
--- /dev/null
@@ -0,0 +1,21 @@
+use strict;\r
+use warnings;\r
+\r
+package TestApp::Template::Any;\r
+\r
+use base 'Template';\r
+use FindBin;\r
+use Path::Class;\r
+\r
+sub new {\r
+    my $class = shift;\r
+\r
+    my $params = defined($_[0]) && ref($_[0]) eq 'HASH' ? shift : {@_};\r
+\r
+    my $includepath = dir($FindBin::Bin, '/lib/TestApp/root/any_include_path');\r
+    $params->{INCLUDE_PATH} = $includepath;\r
+\r
+    return $class->SUPER::new( $params );\r
+}\r
+\r
+1;\r
diff --git a/t/lib/TestApp/View/TT/Classconfig.pm b/t/lib/TestApp/View/TT/Classconfig.pm
new file mode 100644 (file)
index 0000000..4352b5b
--- /dev/null
@@ -0,0 +1,12 @@
+package TestApp::View::TT::Classconfig;
+
+use strict;
+use base 'Catalyst::View::TT';
+
+use TestApp::Template::Any;
+
+__PACKAGE__->config(
+    CLASS              => 'TestApp::Template::Any',
+);
+
+1;
diff --git a/t/lib/TestApp/root/any_include_path/test b/t/lib/TestApp/root/any_include_path/test
new file mode 100644 (file)
index 0000000..706031f
--- /dev/null
@@ -0,0 +1 @@
+[% message %]
\ No newline at end of file