This patch allows HTML::Template to be used with UTF-8 encoded templates.
It works for basic tasks (opening template files and includes), but it
is untested (and probably does not work) with caching.
The patch is (c) Jan "Yenya" Kasprzak ,
http://www.fi.muni.cz/~kas/ and can be distributed
under the same terms as HTML::Template itself.
--- HTML/Template.pm.orig Mon Dec 17 13:21:08 2007
+++ HTML/Template.pm Mon Dec 17 13:35:19 2007
@@ -648,6 +648,20 @@
normal behavior is to look only in the current directory for a
template to include. Defaults to 0.
+=item *
+
+binmode - set the given binmode() when opening the template file and all its
+includes.
+
+Example:
+
+ my $template = HTML::Template->new(
+ filename => 'file.tmpl',
+ binmode => ':utf8'
+ );
+
+NOTE: When the template is given as scalar or filehandle instead of
+file name, this sets the binmode only on included templates).
=back
=item Debugging Options
@@ -1662,13 +1676,16 @@
}
confess("HTML::Template->new() : Cannot open included file $options->{filename} : $!")
- unless defined(open(TEMPLATE, $filepath));
+ unless defined(open(my $fh, $filepath));
$self->{mtime} = $self->_mtime($filepath);
+ if (defined $options->{binmode}) {
+ binmode $fh, $options->{binmode};
+ }
+
# read into scalar, note the mtime for the record
- $self->{template} = "";
- while (read(TEMPLATE, $self->{template}, 10240, length($self->{template}))) {}
- close(TEMPLATE);
+ { local $/; $self->{template} = <$fh>; }
+ close($fh);
} elsif (exists($options->{scalarref})) {
# copy in the template text
@@ -2264,12 +2281,14 @@
die "HTML::Template->new() : Cannot open included file $filename : file not found."
unless defined($filepath);
die "HTML::Template->new() : Cannot open included file $filename : $!"
- unless defined(open(TEMPLATE, $filepath));
+ unless defined(open(my $fh, $filepath));
- # read into the array
- my $included_template = "";
- while(read(TEMPLATE, $included_template, 10240, length($included_template))) {}
- close(TEMPLATE);
+ if ($self->{options}->{binmode}) {
+ binmode $fh, $self->{options}->{binmode};
+ }
+ my $included_template;
+ { local $/; $included_template = <$fh>; }
+ close $fh;
# call filters if necessary
$self->_call_filters(\$included_template) if @{$options->{filter}};