Template-Toolkit
view release on metacpan or search on metacpan
lib/Template/FAQ.pod view on Meta::CPAN
%]
This illustrates the basic principal but you can extend it to perform
pretty much any kind of per-document initialisation that you require.
=head2 Why do I get rubbish for my utf-8 templates?
First of all, make sure that your template files define a Byte Order
Mark L<http://en.wikipedia.org/wiki/Byte_Order_Mark>
If you for some reason don't want to add BOM to your templates, you can
force Template to use a particular encoding (e.g. C<utf8>) for your
templates with the C<ENCODING> option.
my $template = Template->new({
ENCODING => 'utf8'
});
=head1 Questions About This FAQ
=head2 Why is this FAQ so short?
lib/Template/Manual/Config.pod view on Meta::CPAN
=head1 Template Style and Parsing Options
=head2 ENCODING
The C<ENCODING> option specifies the template files' character encoding:
my $template = Template->new({
ENCODING => 'utf8',
});
A template which starts with a Unicode byte order mark (BOM) will have its
encoding detected automatically.
=head2 START_TAG, END_TAG
The C<START_TAG> and C<END_TAG> options are used to specify character
sequences or regular expressions that mark the start and end of inline
template directives. The default values for C<START_TAG> and C<END_TAG> are
'C<[%>' and 'C<%]>' respectively, giving us the familiar directive style:
[% example %]
lib/Template/Provider.pm view on Meta::CPAN
return $time ? 1 : 0 unless defined $load;
return $time
? $load > $time
: $load;
}
#------------------------------------------------------------------------
# _decode_unicode
#
# Decodes encoded unicode text that starts with a BOM and
# turns it into perl's internal representation
#------------------------------------------------------------------------
sub _decode_unicode {
my $self = shift;
my $string = shift;
return undef unless defined $string;
use bytes;
require Encode;
return $string if Encode::is_utf8( $string );
# try all the BOMs in order looking for one (order is important
# 32bit BOMs look like 16bit BOMs)
my $count = 0;
while ($count < @{ $boms }) {
my $enc = $boms->[$count++];
my $bom = $boms->[$count++];
# does the string start with the bom?
if ($bom eq substr($string, 0, length($bom))) {
# decode it and hand it back
lib/Template/Provider.pm view on Meta::CPAN
If a non-existant template is requested through the L<Template>
L<process()|Template#process()> method, or by an C<INCLUDE>, C<PROCESS> or
C<WRAPPER> directive, then the C<DEFAULT> template will instead be processed, if
defined. Note that the C<DEFAULT> template is not used when templates are
specified with absolute or relative filenames, or as a reference to a input
file handle or text string.
=head2 ENCODING
The Template Toolkit will automatically decode Unicode templates that
have a Byte Order Marker (BOM) at the start of the file. This option
can be used to set the default encoding for templates that don't define
a BOM.
my $provider = Template::Provider->new({
ENCODING => 'utf8',
});
See L<Encode> for further information.
=head2 CACHE_SIZE
The L<CACHE_SIZE|Template::Manual::Config#CACHE_SIZE> option can be used to
t/unicode.t view on Meta::CPAN
# delete the contents when we're done with the tempdir, where
# otherwise we just leave the files lying around.
unless (exists $args{dir}) {
$args{dir} = tempdir( CLEANUP => 1 );
}
# work out where we're going to store it
my $temp_filename = catfile($args{dir}, $args{filename});
# open a filehandle with some PerlIO magic to convert data into
# the correct encoding with the correct BOM on the front
open my $temp_fh, ">:raw", $temp_filename
or die "Can't write to '$temp_filename': $!";
# write the data out
print $temp_fh $args{text};
close $temp_fh;
# return where we've created it
return $temp_filename;
}
( run in 0.487 second using v1.01-cache-2.11-cpan-131fc08a04b )