From 74b1933e66642755974629c0f5db623232b0b627 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 4 Oct 2012 12:24:45 +0200 Subject: [PATCH] Bug 8872 Encoding issues in Templates.pm Adds specific encoding parameter when creating Template object. Note that the instruction binmode => ':utf8' could have been added also to the process method. But since it outputs to a Perl variable, this is not needed. Finally, instead of the old encoding of all parameters before processing, the output of process is encoded to UTF-8 octets. Note that Perl could have handled this internally, but this would require adding a use open statement, setting IO layers to UTF-8 in all scripts. Signed-off-by: Jared Camins-Esakov --- C4/Templates.pm | 45 ++------------------------------------------- 1 file changed, 2 insertions(+), 43 deletions(-) diff --git a/C4/Templates.pm b/C4/Templates.pm index 507e077..37bf13d 100644 --- a/C4/Templates.pm +++ b/C4/Templates.pm @@ -67,6 +67,7 @@ sub new { COMPILE_DIR => C4::Context->config('template_cache_dir')?C4::Context->config('template_cache_dir'):'',, INCLUDE_PATH => \@includes, FILTERS => {}, + ENCODING => 'utf8', } ) or die Template->error(); my $self = { @@ -90,7 +91,6 @@ sub output { my $self = shift; my $vars = shift; -# my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename; my $template = $self->{TEMPLATE}; if ( $self->interface eq 'intranet' ) { $vars->{themelang} = '/intranet-tmpl'; @@ -114,56 +114,15 @@ sub output { C4::Context->preference('opaclayoutstylesheet'); # add variables set via param to $vars for processing - # and clean any utf8 mess for my $k ( keys %{ $self->{VARS} } ) { $vars->{$k} = $self->{VARS}->{$k}; - if (ref($vars->{$k}) eq 'ARRAY'){ - utf8_arrayref($vars->{$k}); - } - elsif (ref($vars->{$k}) eq 'HASH'){ - utf8_hashref($vars->{$k}); - } - else { - utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k}); - } } my $data; -# binmode( STDOUT, ":utf8" ); $template->process( $self->filename, $vars, \$data ) || die "Template process failed: ", $template->error(); + utf8::encode($data) if utf8::is_utf8($data); return $data; } - -sub utf8_arrayref { - my $arrayref = shift; - foreach my $element (@$arrayref){ - if (ref($element) eq 'ARRAY'){ - utf8_arrayref($element); - next; - } - if (ref($element) eq 'HASH'){ - utf8_hashref($element); - next; - } - utf8::encode($element) if utf8::is_utf8($element); - } -} - -sub utf8_hashref { - my $hashref = shift; - for my $key (keys %{$hashref}){ - if (ref($hashref->{$key}) eq 'ARRAY'){ - utf8_arrayref($hashref->{$key}); - next; - } - if (ref($hashref->{$key}) eq 'HASH'){ - utf8_hashref($hashref->{$key}); - next; - } - utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key}); - } -} - # FIXME - this is a horrible hack to cache # the current known-good language, temporarily -- 1.7.9.5