Hello,

we are using templates in UTF-8 with Perl 5.8.0.  HTML::Template does
not allow to specify the encoding that templates are in.  I've
implemented perlio_layer option which does exactly that (with
two-arguments binmode call).  See perlopentut for more details.

Also,

 - the code is rearranged a bit to reduce duplication;

 - uc($1) fix is already included in pre-release of HTML::Template
   2.7, but I left it here anyway;


Questions, comments?  [I am not subscribed to the list, btw.]

Index: Template.pm
===================================================================
RCS file: /home/cvs/src/perl-HTML-Template/Template.pm,v
retrieving revision 1.1.1.1
retrieving revision 1.5
diff -u -r1.1.1.1 -r1.5
--- Template.pm 2003/04/30 15:51:51     1.1.1.1
+++ Template.pm 2003/05/28 13:04:24     1.5
@@ -628,6 +628,16 @@
 
 =item *
 
+perlio_layer - if set then do the additional binmode() call to set the
+PerlIO layer (in Perl 5.8.0+).  Usually used to handle templates in
+UTF-8.  Example:
+
+   my $template = HTML::Template->new( filename => 'file.tmpl',
+                                       perlio_layer => ':utf8',
+                                     );
+
+=item *
+
 search_path_on_include - if set to a true value the module will search
 from the top of the array of paths specified by the path option on
 every <TMPL_INCLUDE> and use the first matching template found.  The
@@ -1583,15 +1593,9 @@
       $options->{filepath} = $filepath;   
     }
 
-    confess("HTML::Template->new() : Cannot open included file $options->{filename} : 
$!")
-        unless defined(open(TEMPLATE, $filepath));
+    $self->{template} = $self->_read_template_file($filepath, $options->{filename}, 
$options);
     $self->{mtime} = $self->_mtime($filepath);
 
-    # read into scalar, note the mtime for the record
-    $self->{template} = "";
-    while (read(TEMPLATE, $self->{template}, 10240, length($self->{template}))) {}
-    close(TEMPLATE);
-
   } elsif (exists($options->{scalarref})) {
     # copy in the template text
     $self->{template} = ${$options->{scalarref}};
@@ -1621,6 +1625,28 @@
   return $self;
 }
 
+sub _read_template_file {
+  my $self = shift;
+  my $filepath = shift;
+  my $filename = shift;
+  my $options = shift;
+
+  confess("HTML::Template->new() : Cannot open included file $filename : $!")
+    unless defined(open(TEMPLATE, $filepath));
+
+  if ($options->{perlio_layer}) {
+    binmode TEMPLATE, $options->{perlio_layer}
+      or confess ("HTML::Template->new() : Cannot set PerlIO layer on $filename: $!");
+  }
+
+  # read into scalar
+  my $template = "";
+  while (read(TEMPLATE, $template, 10240, length($template))) {}
+  close(TEMPLATE);
+
+  return $template;
+}
+
 # handle calling user defined filters
 sub _call_filters {
   my $self = shift;
@@ -1907,7 +1933,8 @@
                     (.*) # $19 => $post - text that comes after the tag
                    $/sx) {
 
-      $which = uc($1); # which tag is it
+      $which = $1;
+      $which = uc($which); # which tag is it
 
       $escape = defined $5 ? $5 : defined $15 ? $15 : 0; # escape set?
       
@@ -2177,14 +2204,9 @@
        }
        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));              
-       
-       # read into the array
-       my $included_template = "";
-        while(read(TEMPLATE, $included_template, 10240, length($included_template))) 
{}
-       close(TEMPLATE);
-       
+
+       my $included_template = $self->_read_template_file($filepath, $filename, 
$options);
+
        # call filters if necessary
        $self->_call_filters(\$included_template) if @{$options->{filter}};
        
Index: Changes
===================================================================
RCS file: /home/cvs/src/perl-HTML-Template/Changes,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- Changes     2003/04/30 15:51:51     1.1.1.1
+++ Changes     2003/05/28 12:22:55     1.2
@@ -228,3 +228,5 @@
       - Doc Fix: updated mailing-list information to reflect move from
                  vm.com to sourceforge.net
 
+      - New Feature: perlio_layer option to set the PerlIO layer
+      (e.g., to be used with UTF-8).


--alexm


-------------------------------------------------------
This SF.NET email is sponsored by: eBay
Great deals on office technology -- on eBay now! Click here:
http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
_______________________________________________
Html-template-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/html-template-users

Reply via email to