update for newer catalyst unicode changes 0.42 v0.42
John Napiorkowski [Mon, 29 Dec 2014 19:36:28 +0000 (13:36 -0600)]
Changes
Makefile.PL
lib/Catalyst/View/TT.pm
t/root/heart [new file with mode: 0644]
t/utf8.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index e096a60..20ed68e 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,6 +2,8 @@ Revision history for Perl extension Catalyst::View::TT.
 
 0.42   2014-12-29
         - Fixes to test cases to be compatible with Catalyst v5.90080
+        - Added a Unicode test designed to check new Catalyst function
+        - Docs for above
 
 0.41   2013-02-28
         - New local attribute to let you override the default content type when
index 01cb6f7..2d42524 100644 (file)
@@ -17,6 +17,7 @@ requires 'Path::Class'     => 0;
 requires 'MRO::Compat'     => 0;
 
 test_requires 'Test::More';
+test_requires 'File::Spec';
 
 auto_install;
 resources repository => 'git://git.shadowcat.co.uk/catagits/Catalyst-View-TT.git';
index 39725f9..db72b25 100644 (file)
@@ -453,15 +453,42 @@ 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 additional directories
 to search. See L<CAPTURING TEMPLATE OUTPUT> for an example showing this.
 
-=head2 Unicode
+=head2 Unicode (pre Catalyst v5.90080)
 
 B<NOTE> Starting with L<Catalyst> v5.90080 unicode and encoding has been
 baked into core, and the default encoding is UTF-8.  The following advice
-is for older versions of L<Catalyst>
+is for older versions of L<Catalyst>.
 
 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.
+characters (encoded as utf-8) in your templates.  This is only needed if
+you actually have UTF8 literals in your templates and the BOM is not
+properly set.  Setting encoding here does not magically encode your
+template output.  If you are using this version of L<Catalyst> you need
+to all the Unicode plugin, or upgrade (preferred)
+
+=head2 Unicode (Catalyst v5.90080+)
+
+This version of L<Catalyst> will automatically encode your body output
+to UTF8. This means if your variables contain multibyte characters you don't
+need top do anything else to get UTF8 output.  B<However> if your templates
+contain UTF8 literals (like, multibyte characters actually in the template
+text), then you do need to either set the BOM mark on the template file or
+instruct TT to decode the templates at load time via the ENCODING configuration
+setting.  Most of the time you can just do:
+
+    MyApp::View::HTML->config(
+        ENCODING => 'UTF-8');
+
+and that will just take care of everything.  This configuration setting will
+force L<Template> to decode all files correctly, so that when you hit
+the finalize_encoding step we can properly encode the body as UTF8.  If you
+fail to do this you will get double encoding issues in your output (but again,
+only for the UTF8 literals in your actual template text.)
+
+Again, this ENCODING configuration setting only instructs template toolkit
+how (and if) to decode the contents of your template files when reading them from
+disk.  It has no other effect.
 
 =head2 RENDERING VIEWS
 
diff --git a/t/root/heart b/t/root/heart
new file mode 100644 (file)
index 0000000..0ce50c6
--- /dev/null
@@ -0,0 +1 @@
+<p>This heart literal ♥</p><p>This is heart var [% hearts%]</p>
diff --git a/t/utf8.t b/t/utf8.t
new file mode 100644 (file)
index 0000000..628a7e6
--- /dev/null
+++ b/t/utf8.t
@@ -0,0 +1,49 @@
+use utf8;
+use warnings;
+use strict;
+use Test::More;
+use Encode 2.21 'decode_utf8', 'encode_utf8';
+use HTTP::Request::Common;
+use File::Spec;
+
+{
+  package MyApp::Controller::Root;
+  $INC{'MyApp/Controller/Root.pm'} = __FILE__;
+
+  use base 'Catalyst::Controller';
+
+  sub heart :Path('♥') {
+    my ($self, $c) = @_;
+    $c->stash(hearts=>'♥♥♥');
+    $c->detach('View::HTML');
+  }
+  package MyApp::View::HTML;
+  $INC{'MyApp/View/HTML.pm'} = __FILE__;
+
+  use base 'Catalyst::View::TT';
+
+  MyApp::View::HTML->config(
+    ENCODING => 'UTF-8',
+    INCLUDE_PATH=> File::Spec->catfile('t'));
+
+  package MyApp;
+  use Catalyst;
+
+  MyApp->setup;
+}
+
+use Catalyst::Test 'MyApp';
+
+if(MyApp->can('encoding') and MyApp->can('clear_encoding') ) {
+  ok my $res = request GET '/root/♥', 'Accept-Encoding' => 'gzip';
+
+  is $res->code, 200, 'OK';
+  
+  is decode_utf8($res->content), "<p>This heart literal ♥</p><p>This is heart var ♥♥♥</p>\n", 'correct body';
+  is $res->content_charset, 'UTF-8';
+} else {
+  ok 1, 'Skipping the UTF8 Tests for older installed catalyst';
+}
+
+done_testing;
+