Template::Provider::Encode - Encode templates for Template Toolkit


Template-Provider-Encode documentation Contained in the Template-Provider-Encode distribution.

Index


Code Index:

NAME

Top

Template::Provider::Encode - Encode templates for Template Toolkit

SYNOPSIS

Top

  use Template::Provider::Encode;
  use Template;
  my $tt = Template->new(
      LOAD_TEMPLATES => [Template::Provider::Encode::UTF8->new({ie=>'shiftjis'
                                                                oe=>'utf8'})]
  );
  my $author = "\xe3\x81\x9b\xe3\x81\x8d\xe3\x82\x80\xe3\x82\x89";
  $tt->process('t/tmpl/SJIS.tt2', {author => $author});

DESCRIPTION

Top

TBW

SEE ALSO

Top

Encode, Template::Provider

AUTHOR

Top

Masayoshi Sekimura, <sekimura at gmail dot com>

COPYRIGHT AND LICENSE

Top


Template-Provider-Encode documentation Contained in the Template-Provider-Encode distribution.

package Template::Provider::Encode;
use strict;
use warnings;

use base qw(Template::Provider);
use Encode;

our $VERSION = '0.02';
our $INPUT_ENCODING;
our $OUTPUT_ENCODING;

sub new {
    my $class = shift;
    my $options = shift;

    $INPUT_ENCODING  = exists $options->{ie} ? $options->{ie} : undef; 
    $OUTPUT_ENCODING = exists $options->{oe} ? $options->{oe} : undef; 
    delete $options->{ie};
    delete $options->{oe};

    return $class->SUPER::new($options);
}

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

    if ($INPUT_ENCODING and $OUTPUT_ENCODING) {
        Encode::from_to($data->{text}, $INPUT_ENCODING, $OUTPUT_ENCODING );
    }

    return ($data, $error);
}

1;
__END__