From: John Napiorkowski Date: Thu, 28 Feb 2013 22:03:45 +0000 (-0500) Subject: new config attr that lets you override the default content type X-Git-Tag: 0.41~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-View-TT.git;a=commitdiff_plain;h=59a2e92ae5c162501fca685ee55406b0ed5fe4a2 new config attr that lets you override the default content type --- diff --git a/Changes b/Changes index d3eeacd..6d3c5d8 100644 --- 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) diff --git a/lib/Catalyst/View/TT.pm b/lib/Catalyst/View/TT.pm index a8a1b27..c0ac5b6 100644 --- a/lib/Catalyst/View/TT.pm +++ b/lib/Catalyst/View/TT.pm @@ -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. + +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 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 index 0000000..bed2f0f --- /dev/null +++ b/t/14alt_content_type.t @@ -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; diff --git a/t/lib/TestApp/Controller/Root.pm b/t/lib/TestApp/Controller/Root.pm index 92105a5..7d612c2 100644 --- a/t/lib/TestApp/Controller/Root.pm +++ b/t/lib/TestApp/Controller/Root.pm @@ -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 index 0000000..a3e5581 --- /dev/null +++ b/t/lib/TestApp/View/TT/AltContentType.pm @@ -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 index 0000000..706031f --- /dev/null +++ b/t/lib/TestApp/root/test_alt_content_type.tt @@ -0,0 +1 @@ +[% message %] \ No newline at end of file