Главная страница


ru.cgi.perl

 
 - RU.CGI.PERL ------------------------------------------------------------------
 From : Alexey Mahotkin                      2:5020/400     29 May 2003  00:53:58
 To : All
 Subject : HTML::Template и шаблоны в UTF-8
 -------------------------------------------------------------------------------- 
 
 
 сегодня наконец добрался до шаблонов в UTF-8.  Чтобы оно заработало в общем
 случае, потребовалось добавить в конструктор HTML::Template новый
 параметр.  Как это сделать меньшей кровью -- неясно.  Использование PerlIO,
 впрочем, открывает тонну новых возможностей :)
 Вот патч:
 
 To: sam(@)tregar.com
 Cc: html-template-users(@)lists.sourceforge.net
 Subject: templates in UTF-8 (and generic support for PerlIO layers)
 Date: Wed, 28 May 2003 17:06:50 +0400
 X-Gnus-Mail-Source: file:/var/mail/alexm
 Message-ID: <8765nvrzit.fsf@dim.w-m.ru>
 
 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
 
 --- ifmail v.2.15dev5
  * Origin: tyranny (2:5020/400)
 
 

Вернуться к списку тем, сортированных по:

 Тема:    Автор:    Дата:  
 HTML::Template и шаблоны в UTF-8   Alexey Mahotkin   29 May 2003 00:53:58 
 Re: HTML::Template и шаблоны в UTF-8   Bash   21 Jun 2003 05:22:29 
Архивное /ru.cgi.perl/386514b5be86.html, оценка 3 из 5, голосов 10
Яндекс.Метрика
Valid HTML 4.01 Transitional