I have some problem with unicode, and I didn't find any right solution
in the documentation.
In my project all data is in utf-8 encoding, and all strings has UTF8
flag set.
And all templates is also in utf-8.
When I tries to insert utf-8 value in utf-8 template, all non-latin
character in template is becoming a garbage, like this
"ÐааÐббÐвв" (as if it were passed through iconv -f iso8859-1)
Here is an example:
=== utf-example.pl ===================
#!/usr/bin/perl
use strict;
use Template;
use utf8; # Should not remove this, 'cause it add UTF8 flag to all ""
strings that needs it use open ':utf8';
my $template = Template->new();
my $value = "Utf-8 charactes in inserted value: ГггДддЕее";
# my $value = "No utf-8 characters here"; # This will work well
my $vars ={value =>$value};
$template->process('utf-example.tmpl', $vars)
|| die "Template process failed: ", $template->error(), "\n";
===========================
== utf-example.tmpl =================
UTF-8 characters inside template: АааБббВвв
Value = [% GET value %]
=====================================
The origin of the problem is that $template->process do not set UTF8
flag on scalar with template text, and perl when it is offered to merge
two strings one with UTF8 flag and one without, will try to convert all
strings to utf-8. That will cause the corruption of template data,
because it is already in utf-8, only UTF8 flag is missing.
One of the workarounds for this problem is to remove UTF8 flag from all
values that substed in the template:
my $vars ={value =>_remove_utf8_flag($value)};
sub _remove_utf8_flag
{
my $s=shift;
Encode::_utf8_off($s);
return($s);
}
This will get all output into correct unicode. But this is not the best
idea. Are there some official utf-8 solutions for Template Toolkit?
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates