>>>>> "SW" == Sam Wong <[EMAIL PROTECTED]> writes:

SW> Currently I am working on TT 2.1 and perl 5.8.0.

SW> The template I am using contains unicode char and encoded as
SW> UTF-8.

SW> But perl seems dun aware that my template is utf-8 and it treat it
SW> in raw-byte mode. How can I make it works?

The only way to use utf-8 templates I found is to subclass
Template::Provider and overload private method _load.

Here code we used to do this:

package Datamodel::Template::Provider;

use base qw(Template::Provider);

use Datamodel::Tools qw(utf8_upgrade);

sub _load {
    my $self = shift;

    my ($data, $error) = $self->SUPER::_load(@_);

    if(defined $data) {
        $data->{text} = utf8_upgrade($data->{text});
    }

    return ($data, $error);
}

utf8_upgrade is defined as

# converts UTF-8 byte sequence to native Perl UTF-8 string
sub utf8_upgrade {
    my @list = map pack('U*', unpack 'U0U*', $_), @_;
    return wantarray ? @list : $list[0];
}

I think utf8_upgrade can be replaced with one of funcs from utf8.pm
put we were using it for hysterical reasons.

Once you have subclassed Template::Provider you should use it with
Template object.

Template->new({ LOAD_TEMPLATES => [ Datamodel::Template::Provider->new ] });

Also I found that that you cannot use Template::Stash::XS with utf-8
strings. If it is enabled by default make sure you revert to pure Perl
version:


Template->new({ STASH          => Template::Stash->new,
                LOAD_TEMPLATES => [ Datamodel::Template::Provider->new ] });

-- 
Ilya Martynov,  [EMAIL PROTECTED]
CTO IPonWEB (UK) Ltd
Quality Perl Programming and Unix Support
UK managed @ offshore prices - http://www.iponweb.net
Personal website - http://martynov.org

_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to