root/Template-Provider-Encoding/trunk/lib/Template/Provider/Encoding.pm

Revision 2045 (checked in by miyagawa, 13 years ago)

this be 0.06

  • Property svn:keywords set to Id Revision
Line 
1 package Template::Provider::Encoding;
2
3 use strict;
4 our $VERSION = '0.06';
5
6 use base qw( Template::Provider );
7 use Encode;
8
9 sub _load {
10     my $self = shift;
11     my($data, $error) = $self->SUPER::_load(@_);
12
13     unless (Encode::is_utf8($data->{text})) {
14         my $encoding = $data->{text} =~ /^\[% USE encoding '([\w\-]+)'/
15             ? $1 : 'utf-8';
16         $data->{text} = Encode::decode($encoding, $data->{text});
17     }
18
19     return ($data, $error);
20 }
21
22 1;
23 __END__
24
25 =head1 NAME
26
27 Template::Provider::Encoding - Explicitly declare encodings of your templates
28
29 =head1 SYNOPSIS
30
31   use Template::Provider::Encoding;
32   use Template::Stash::ForceUTF8;
33   use Template;
34
35   my $tt = Template->new(
36       LOAD_TEMPLATES => [ Template::Provider::Encoding->new ],
37       STASH => Template::Stash::ForceUTF8->new,
38   );
39
40   # Everything should be Unicode
41   # (but you can pass UTF-8 bytes as well, thanks to Template::Stash::ForceUTF8)
42   my $author = "\x{5bae}\x{5ddd}";
43
44   # this will emit Unicode flagged string to STDOUT. You might
45   # probably want to binmode(STDOUT, ":encoding($enccoding)")
46   # before process() call
47   $tt->process($template, { author => $author });
48
49   # in your templates
50   [% USE encoding 'utf-8' -%]
51   My name is [% author %]. { ... whatever UTF-8 bytes }
52
53 =head1 DESCRIPTION
54
55 Template::Provider::Encoding is a Template Provider subclass to decode
56 template using its declaration. You have to declare encoding of the
57 template in the head (1st line) of template using (fake) encoding TT
58 plugin. Otherwise the template is handled as utf-8.
59
60   [% USE encoding 'utf-8' %]
61   Here comes utf-8 strings with [% variable %].
62
63 =head1 DIFFERNCE WITH OTHER WAYS
64
65 =head2 UNICODE option and BOM
66
67 Recent TT allows C<UNICODE> option to Template::Provider and by adding
68 it Provider scans BOM (byte-order mark) to detect UTF-8/UTF-16 encoded
69 template files. This module does basically the same thing in a
70 different way, but IMHO adding BOM to template files is a little
71 painful especially for non-programmers.
72
73 =head2 Template::Provider::Encode
74
75 L<Template::Provider::Encode> provides a very similar way to detect
76 Template file encodings and output the template into various
77 encodings.
78
79 This module doesn't touch output encoding of the template and instead
80 it emits valid Unicode flagged string. I think the output encoding
81 conversion should be done by other piece of code, especially in the
82 framework.
83
84 This module doesn't require you to specify encoding in the code, nor
85 doesn't I<guess> encodings. Instead it forces you to put C<< [% USE
86 encoding 'foo-bar' %] >> in the top of template files, which is
87 explicit and, I think, is a good convention.
88
89 =head1 AUTHOR
90
91 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
92
93 This library is free software; you can redistribute it and/or modify
94 it under the same terms as Perl itself.
95
96 =head1 SEE ALSO
97
98 L<Template::Stash::ForceUTF8>, L<Template::Provider::Encode>
99
100 =cut
Note: See TracBrowser for help on using the browser.