1Template::Provider::EncUosdeirngC(o3n)tributed Perl DocuTmeemnptlaattieo:n:Provider::Encoding(3)
2
3
4
6 Template::Provider::Encoding - Explicitly declare encodings of your
7 templates
8
10 use Template::Provider::Encoding;
11 use Template::Stash::ForceUTF8;
12 use Template;
13
14 my $tt = Template->new(
15 LOAD_TEMPLATES => [ Template::Provider::Encoding->new ],
16 STASH => Template::Stash::ForceUTF8->new,
17 );
18
19 # Everything should be Unicode
20 # (but you can pass UTF-8 bytes as well, thanks to Template::Stash::ForceUTF8)
21 my $author = "\x{5bae}\x{5ddd}";
22
23 # this will emit Unicode flagged string to STDOUT. You might
24 # probably want to binmode(STDOUT, ":encoding($enccoding)")
25 # before process() call
26 $tt->process($template, { author => $author });
27
28 # in your templates
29 [% USE encoding 'utf-8' -%]
30 My name is [% author %]. { ... whatever UTF-8 bytes }
31
33 Template::Provider::Encoding is a Template Provider subclass to decode
34 template using its declaration. You have to declare encoding of the
35 template in the head (1st line) of template using (fake) encoding TT
36 plugin. Otherwise the template is handled as utf-8.
37
38 [% USE encoding 'utf-8' %]
39 Here comes utf-8 strings with [% variable %].
40
42 UNICODE option and BOM
43 Recent TT allows "UNICODE" option to Template::Provider and by adding
44 it Provider scans BOM (byte-order mark) to detect UTF-8/UTF-16 encoded
45 template files. This module does basically the same thing in a
46 different way, but IMHO adding BOM to template files is a little
47 painful especially for non-programmers.
48
49 Template::Provider::Encode
50 Template::Provider::Encode provides a very similar way to detect
51 Template file encodings and output the template into various encodings.
52
53 This module doesn't touch output encoding of the template and instead
54 it emits valid Unicode flagged string. I think the output encoding
55 conversion should be done by other piece of code, especially in the
56 framework.
57
58 This module doesn't require you to specify encoding in the code, nor
59 doesn't guess encodings. Instead it forces you to put "[% USE encoding
60 'foo-bar' %]" in the top of template files, which is explicit and, I
61 think, is a good convention.
62
64 Tatsuhiko Miyagawa <miyagawa@bulknews.net>
65
66 This library is free software; you can redistribute it and/or modify it
67 under the same terms as Perl itself.
68
70 Template::Stash::ForceUTF8, Template::Provider::Encode
71
72
73
74perl v5.38.0 2023-07-21 Template::Provider::Encoding(3)