Template-Toolkit
view release on metacpan or search on metacpan
lib/Template/Provider.pm view on Meta::CPAN
# Returns the content in scalar context.
# Returns ($data, $error, $mtime) in list context where
# $data - content
# $error - error string if there was an error, otherwise undef
# $mtime - last modified time from calling stat() on the path
#------------------------------------------------------------------------
sub _template_content {
my ($self, $path) = @_;
return (undef, "No path specified to fetch content from ")
unless $path;
my $data;
my $mod_date;
my $error;
local *FH;
if(-d $path) {
$error = "$path: not a file";
}
elsif (open(FH, "<", $path)) {
local $/;
binmode(FH);
$data = <FH>;
$mod_date = (stat($path))[9];
close(FH);
}
else {
$error = "$path: $!";
}
return wantarray
? ( $data, $error, $mod_date )
: $data;
}
#------------------------------------------------------------------------
# _modified($name)
# _modified($name, $time)
#
# When called with a single argument, it returns the modification time
# of the named template. When called with a second argument it returns
# true if $name has been modified since $time.
#------------------------------------------------------------------------
sub _modified {
my ($self, $name, $time) = @_;
my $load = $self->_template_modified($name);
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
return Encode::decode($enc, substr($string, length($bom)), 1);
}
}
return $self->{ ENCODING }
? Encode::decode( $self->{ ENCODING }, $string )
: $string;
}
1;
__END__
=head1 NAME
Template::Provider - Provider module for loading/compiling templates
=head1 SYNOPSIS
$provider = Template::Provider->new(\%options);
($template, $error) = $provider->fetch($name);
=head1 DESCRIPTION
The L<Template::Provider> is used to load, parse, compile and cache template
documents. This object may be sub-classed to provide more specific facilities
for loading, or otherwise providing access to templates.
The L<Template::Context> objects maintain a list of L<Template::Provider>
objects which are polled in turn (via L<fetch()|Template::Context#fetch()>) to
return a requested template. Each may return a compiled template, raise an
error, or decline to serve the request, giving subsequent providers a chance
to do so.
The L<Template::Provider> can also be subclassed to provide templates from
a different source, e.g. a database. See L<SUBCLASSING> below.
This documentation needs work.
=head1 PUBLIC METHODS
=head2 new(\%options)
Constructor method which instantiates and returns a new C<Template::Provider>
object. A reference to a hash array of configuration options may be passed.
See L<CONFIGURATION OPTIONS> below for a summary of configuration options
and L<Template::Manual::Config> for full details.
lib/Template/Provider.pm view on Meta::CPAN
=head2 INCLUDE_PATH
The L<INCLUDE_PATH|Template::Manual::Config#INCLUDE_PATH> option is used to
specify one or more directories in which template files are located.
# single path
my $provider = Template::Provider->new({
INCLUDE_PATH => '/usr/local/templates',
});
# multiple paths
my $provider = Template::Provider->new({
INCLUDE_PATH => [ '/usr/local/templates',
'/tmp/my/templates' ],
});
=head2 ABSOLUTE
The L<ABSOLUTE|Template::Manual::Config#ABSOLUTE> flag is used to indicate if
templates specified with absolute filenames (e.g. 'C</foo/bar>') should be
processed. It is disabled by default and any attempt to load a template by
such a name will cause a 'C<file>' exception to be raised.
my $provider = Template::Provider->new({
ABSOLUTE => 1,
});
=head2 RELATIVE
The L<RELATIVE|Template::Manual::Config#RELATIVE> flag is used to indicate if
templates specified with filenames relative to the current directory (e.g.
C<./foo/bar> or C<../../some/where/else>) should be loaded. It is also disabled
by default, and will raise a C<file> error if such template names are
encountered.
my $provider = Template::Provider->new({
RELATIVE => 1,
});
=head2 DEFAULT
The L<DEFAULT|Template::Manual::Config#DEFAULT> option can be used to specify
a default template which should be used whenever a specified template can't be
found in the L<INCLUDE_PATH>.
my $provider = Template::Provider->new({
DEFAULT => 'notfound.html',
});
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
limit the number of compiled templates that the module should cache. By
default, the L<CACHE_SIZE|Template::Manual::Config#CACHE_SIZE> is undefined
and all compiled templates are cached.
my $provider = Template::Provider->new({
CACHE_SIZE => 64, # only cache 64 compiled templates
});
=head2 STAT_TTL
The L<STAT_TTL|Template::Manual::Config#STAT_TTL> value can be set to control
how long the C<Template::Provider> will keep a template cached in memory
before checking to see if the source template has changed.
my $provider = Template::Provider->new({
STAT_TTL => 60, # one minute
});
=head2 COMPILE_EXT
The L<COMPILE_EXT|Template::Manual::Config#COMPILE_EXT> option can be
provided to specify a filename extension for compiled template files.
It is undefined by default and no attempt will be made to read or write
any compiled template files.
my $provider = Template::Provider->new({
COMPILE_EXT => '.ttc',
});
=head2 COMPILE_DIR
The L<COMPILE_DIR|Template::Manual::Config#COMPILE_DIR> option is used to
specify an alternate directory root under which compiled template files should
be saved.
my $provider = Template::Provider->new({
COMPILE_DIR => '/tmp/ttc',
});
=head2 TOLERANT
The L<TOLERANT|Template::Manual::Config#TOLERANT> flag can be set to indicate
that the C<Template::Provider> module should ignore any errors encountered while
loading a template and instead return C<STATUS_DECLINED>.
=head2 PARSER
The L<PARSER|Template::Manual::Config#PARSER> option can be used to define
a parser module other than the default of L<Template::Parser>.
( run in 0.384 second using v1.01-cache-2.11-cpan-5a3173703d6 )