new config attr that lets you override the default content type
John Napiorkowski [Thu, 28 Feb 2013 22:03:45 +0000 (17:03 -0500)]
Changes
lib/Catalyst/View/TT.pm
t/14alt_content_type.t [new file with mode: 0644]
t/lib/TestApp/Controller/Root.pm
t/lib/TestApp/View/TT/AltContentType.pm [new file with mode: 0755]
t/lib/TestApp/root/test_alt_content_type.tt [new file with mode: 0644]

diff --git a/Changes b/Changes
index d3eeacd..6d3c5d8 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 Revision history for Perl extension Catalyst::View::TT.
 
+0.41   TBA
+        - New local attribute to let you override the default content type when
+          no content type has been set for the response.
 0.40   2013-01-15 20:52:14
         - Fix hash randomisation breakage in tests (RT#82703)
 
index a8a1b27..c0ac5b6 100644 (file)
@@ -16,6 +16,7 @@ $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;
 
@@ -229,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);
@@ -624,6 +626,18 @@ Then in the template:
 
   [% uri_for_css('home.css') %]
 
+=head2 content_type
+
+This lets you override the default content type for the response.  If you do
+not set this and if you do not set the content type in your controllers, the
+default is C<text/html; charset=utf-8>.
+
+Use this if you are creating alternative view responses, such as text or JSON
+and want a global setting.
+
+Any content type set in your controllers before calling this view are respected
+and have priority.
+
 =head2 C<CATALYST_VAR>
 
 Allows you to change the name of the Catalyst context object. If set, it will also
diff --git a/t/14alt_content_type.t b/t/14alt_content_type.t
new file mode 100644 (file)
index 0000000..bed2f0f
--- /dev/null
@@ -0,0 +1,11 @@
+use strict;
+use warnings;
+use Test::More;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use_ok('Catalyst::Test', 'TestApp');
+is(request("/test_alt_content_type")->header('Content-Type'), 'text/plain');
+
+done_testing;
index 92105a5..7d612c2 100644 (file)
@@ -53,6 +53,12 @@ sub test_msg : Local {
     $c->stash->{template} = 'test.tt';
 }
 
+sub test_alt_content_type : Local {
+    my ($self, $c) = @_;
+    $c->stash( message => 'test_alt_content_type');
+    $c->forward('View::TT::AltContentType');
+}
+
 sub end : Private {
     my ($self, $c) = @_;
 
diff --git a/t/lib/TestApp/View/TT/AltContentType.pm b/t/lib/TestApp/View/TT/AltContentType.pm
new file mode 100755 (executable)
index 0000000..a3e5581
--- /dev/null
@@ -0,0 +1,9 @@
+package TestApp::View::TT::AltContentType;
+
+use strict;
+use base 'Catalyst::View::TT';
+
+__PACKAGE__->config(
+  TEMPLATE_EXTENSION => '.tt', 
+  content_type => 'text/plain',
+);
diff --git a/t/lib/TestApp/root/test_alt_content_type.tt b/t/lib/TestApp/root/test_alt_content_type.tt
new file mode 100644 (file)
index 0000000..706031f
--- /dev/null
@@ -0,0 +1 @@
+[% message %]
\ No newline at end of file