Skip Menu |

This queue is for tickets about the HTML-Template CPAN distribution.

Report information
The Basics
Id: 30586
Status: resolved
Priority: 0/
Queue: HTML-Template

People
Owner: Nobody in particular
Requestors: moritz [...] faui2k3.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 2.9
Fixed in: (no value)



Subject: Allow the user to specify an open mode [PATCH]
Attached is a patch against the subversion repository at revision 327. It implements a possibility to influence the way HTML::Template loads files (e.g. respecting encoding, line endings), and comes with a test file for that. If you like the patch, I'd be happy to see it main HTML::Template. Please take a look at the way file_cache is tested, I'm not entirely sure that I used the right options to force file caching. The patch has to be applied in a clean utf8 environment, otherwise the file 'templates/utf8-test.tmpl' might have an incorrect charset.
Subject: open_mode.patch
Index: t/12-open_mode.t =================================================================== --- t/12-open_mode.t (revision 0) +++ t/12-open_mode.t (revision 0) @@ -0,0 +1,55 @@ +use strict; +use warnings; +use Test::More; +use File::Temp qw(tempdir); + +use HTML::Template; + +BEGIN { + if ($] < 5.007001){ + plan(skip_all => 'open_mode needs at least perl 5.7.1'); + } else { + plan(tests => 2); + } +} + +my $test_fn = 'templates/utf8-test.tmpl'; + +die "Can't find test file '$test_fn' " unless -e $test_fn; + +my $tmpl = HTML::Template->new( + filename => $test_fn, + open_mode => '<:encoding(utf-8)', + ); +my $output = $tmpl->output; +chomp $output; + +is($output, chr(228)); + +my $cache_dir = tempdir( CLEANUP => 1); + +# same as before, this time we test file_cache +$tmpl = HTML::Template->new( + filename => $test_fn, + open_mode => '<:encoding(utf-8)', + cache => 0, + file_cache => 1, + file_cache_dir => $cache_dir, + ); + +# trigger cache storage: +$output = $tmpl->output; + +# this time it will implicitly read from the cache +$tmpl = HTML::Template->new( + filename => $test_fn, + open_mode => '<:encoding(utf-8)', + cache => 0, + file_cache => 1, + file_cache_dir => $cache_dir, + ); + +$output = $tmpl->output; +chomp $output; + +is($output, chr(228)); Index: Template.pm =================================================================== --- Template.pm (revision 327) +++ Template.pm (working copy) @@ -642,6 +642,25 @@ =item * +open_mode - you can set this option to an opening mode with which all +template files will be opened. + +For example: + + my $template = HTML::Template->new( filename => 'file.tmpl', + open_mode => '<:encoding(utf-8)' + ); + +That way you can force a charset, CR/LF properties etc. on the template +files. See L<PerlIO> for details. + +NOTE: this only works in perl 5.7.1 and above. + +SECOND NOTE: you have to supply an opening mode that actually permits +reading from the file handle. + +=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 @@ -965,6 +984,7 @@ die_on_bad_params => 1, vanguard_compatibility_mode => 0, associate => [], + open_mode => '', path => [], strict => 1, loop_context_vars => 0, @@ -1661,8 +1681,17 @@ $options->{filepath} = $filepath; } - confess("HTML::Template->new() : Cannot open included file $options->{filename} : $!") - unless defined(open(TEMPLATE, $filepath)); + { + my $open_ret_value; + # no 3 args form of open before perl 5.7.1 + if (length($options->{open_mode}) && $] >= 5.007001){ + $open_ret_value = open(TEMPLATE, $options->{open_mode}, $filepath); + } else { + $open_ret_value = open(TEMPLATE, $filepath); + } + confess("HTML::Template->new() : Cannot open included file $options->{filename} : $!") + unless defined($open_ret_value); + } $self->{mtime} = $self->_mtime($filepath); # read into scalar, note the mtime for the record Index: templates/utf8-test.tmpl =================================================================== --- templates/utf8-test.tmpl (revision 0) +++ templates/utf8-test.tmpl (revision 0) @@ -0,0 +1 @@ +ä \ No newline at end of file
I've done something similar in the past to handle encodings, but I'm wondering if we shouldn't simplify it with an "encoding" parameter that could just be "utf-8" and use the open mode behind the scenes. It keeps the interface simple and doesn't require the user to learn about Perl IO layers. On the other hand, it prevents using IO layers for other trickery like line endings (as you mentioned). Maybe I should implement both.
open_mode option will appear in HTML::Template 2.10 which will be released soon. I had to tweak a couple of things so that the cache would also take open_mode into effect, but your patches were a good start.