Le 7 juil. 05 à 10:07, Oleg Burlaca a écrit :

Hi,


I've solved my problem by adding "use open 'utf8';" at the beginning of Template::Provider

I don't think it is a good solution, because it will affect all projects that use TT on the machine

I've searched better about this issue and found:
http://template-toolkit.org/pipermail/templates/2004-June/006270.html

you'll have to add a BOM to every utf template. (also make sure you have TT 2.14)


I also found this Template-InputStreamDecoder module, that don't require you to add that BOM,
but it is also a not so good solution.

have a look at Template::Provider ...

my $boms = [
        'UTF-8'         => "\x{ef}\x{bb}\x{bf}",
        'UTF-32BE'      => "\x{0}\x{0}\x{fe}\x{ff}",
        'UTF-32LE'      => "\x{ff}\x{fe}\x{0}\x{0}",
        'UTF-16BE'      => "\x{fe}\x{ff}",
        'UTF-16LE'      => "\x{ff}\x{fe}",
];

If you don't want to deal with BOMs, or if you're dealing with non unicode encodings,
there are other solutions (TT is so flexible!).
In an application where I have templates in different encodings, I specify the encoding as an option to my Template subclass' constructor, and pass that on to a Template::Parser
subclass.

    $template = MyTemplate->new(ENCODING => 'ISO-8859-6');
    ...

    package MyTemplate;
    use base qw(Template);
    use MyTemplateParser;

    sub _init
    {
        my ($self, $options) = @_;
        $options->{PARSER} = MyTemplateParser->new($options);
        ...
        $self->SUPER::_init($options)
    }
    ...
    package MyTemplateParser;
    use base qw(Template::Parser);
    use Encode;

    sub new
    {
        my ($class, $options) = @_;
        my $self = $class->SUPER::new($options);
        $self->{ENCODING} = $options->{ENCODING};
        return $self;
    }
    sub parse
    {
        my ($self, $text) = @_;
        $text = Encode::decode($self->{ENCODING}, $text);
        return $self->SUPER::parse ($text);
    }

I'm sure there are other ways, I used the parser because I already subclassed it for
other reasons.

--
Eric Cholet



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

Reply via email to