Template::Provider::Encoding - Explicitly declare encodings of your templates
use Template::Provider::Encoding; use Template::Stash::ForceUTF8; use Template; my $tt = Template->new( LOAD_TEMPLATES => [ Template::Provider::Encoding->new ], STASH => Template::Stash::ForceUTF8->new, ); # Everything should be Unicode # (but you can pass UTF-8 bytes as well, thanks to Template::Stash::ForceUTF8) my $author = "\x{5bae}\x{5ddd}"; # this will emit Unicode flagged string to STDOUT. You might # probably want to binmode(STDOUT, ":encoding($enccoding)") # before process() call $tt->process($template, { author => $author }); # in your templates [% USE encoding 'utf-8' -%] My name is [% author %]. { ... whatever UTF-8 bytes }
Template::Provider::Encoding is a Template Provider subclass to decode template using its declaration. You have to declare encoding of the template in the head (1st line) of template using (fake) encoding TT plugin. Otherwise the template is handled as utf-8.
[% USE encoding 'utf-8' %] Here comes utf-8 strings with [% variable %].
Recent TT allows UNICODE option to Template::Provider and by adding it Provider scans BOM (byte-order mark) to detect UTF-8/UTF-16 encoded template files. This module does basically the same thing in a different way, but IMHO adding BOM to template files is a little painful especially for non-programmers.
UNICODE
Template::Provider::Encode provides a very similar way to detect Template file encodings and output the template into various encodings.
This module doesn't touch output encoding of the template and instead it emits valid Unicode flagged string. I think the output encoding conversion should be done by other piece of code, especially in the framework.
This module doesn't require you to specify encoding in the code, nor doesn't guess encodings. Instead it forces you to put [% USE encoding 'foo-bar' %] in the top of template files, which is explicit and, I think, is a good convention.
[% USE encoding 'foo-bar' %]
Tatsuhiko Miyagawa <miyagawa@bulknews.net>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Template::Stash::ForceUTF8, Template::Provider::Encode
To install Template::Provider::Encoding, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Template::Provider::Encoding
CPAN shell
perl -MCPAN -e shell install Template::Provider::Encoding
For more information on module installation, please visit the detailed CPAN module installation guide.