Subject: | UTF-8 issues with TT |
Hi Stefan,
I've just fixed a problem my app had with UTF-8 encoded templates in the default config, maybe you'd like to document that. To reproduce the bug, which is actually a TT problem:
- Whip up a test app with "Kelp TestApp"
- Change views/hello.tt to read "Hello, ☃"
- Change the config route in lib/TestApp.pm to
sub { return "Hello, ☃"; }
and add a "use utf8;" below the package declaration.
- Start plackup
- Access both /home and /config with a browser
The /config route's output has the snowman rendered correctly, the /home one reads "Hello, â".
The Unicode snowman is U+2603 or e2 98 83 in UTF-8; e2 is â in Latin-1 while 98 iand 83 are undefined so they're not displayed (you get the other two bytes copied to the clipboard if you select the text in the browser though)
Turns out TT can't properly deduce the template's encoding unless it has a BOM so writing to a file produces the correct output but rendering to a string doesn't set the UTF8 flag as it should (by the way, the "binmode => ':utf8'" in Kelp::Module::Template, while doing no harm, is redundant when rendering to a string). One can help TT by adding an entry to modules_init in the config file:
Template => {
ENCODING => 'utf8',
}
The ENCODING parameter is undocumented in TT's current CPAN version (cf. https://rt.cpan.org/Public/Bug/Display.html?id=64694) and I suppose UTF-8 for templates is a common choice so it would help having that documented or even added to the config already by the Kelp script.
cheers,
Matthias