Template::Provider - Provider module for loading/compiling templates |
Template::Provider - Provider module for loading/compiling templates
$provider = Template::Provider->new(\%options);
($template, $error) = $provider->fetch($name);
The 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 Template::Context objects maintain a list of Template::Provider
objects which are polled in turn (via fetch())
to return a requested
template. Each may return a compiled template, raise an error, or
decline to serve the reqest, giving subsequent providers a chance to
do so.
This is the ``Chain of Responsiblity'' pattern. See 'Design Patterns' for further information.
This documentation needs work.
new(\%options)
Constructor method which instantiates and returns a new Template::Provider object. The optional parameter may be a hash reference containing any of the following items:
my $provider = Template::Provider->new({ INCLUDE_PATH => '/usr/local/templates', });
my $provider = Template::Provider->new({ INCLUDE_PATH => '/usr/local/templates:/tmp/my/templates', });
my $provider = Template::Provider->new({ INCLUDE_PATH => [ '/usr/local/templates', '/tmp/my/templates' ], });
On Win32 systems, a little extra magic is invoked, ignoring delimiters that have ':' followed by a '/' or '\'. This avoids confusion when using directory names like 'C:\Blah Blah'.
When specified as a list, the INCLUDE_PATH path can contain elements
which dynamically generate a list of INCLUDE_PATH directories. These
generator elements can be specified as a reference to a subroutine or
an object which implements a paths()
method.
my $provider = Template::Provider->new({ INCLUDE_PATH => [ '/usr/local/templates', \&incpath_generator, My::IncPath::Generator->new( ... ) ], });
Each time a template is requested and the INCLUDE_PATH examined, the
subroutine or object method will be called. A reference to a list of
directories should be returned. Generator subroutines should report
errors using die(). Generator objects should return undef and make an
error available via its error()
method.
For example:
sub incpath_generator {
# ...some code...
if ($all_is_well) { return \@list_of_directories; } else { die "cannot generate INCLUDE_PATH...\n"; } }
or:
package My::IncPath::Generator;
# Template::Base (or Class::Base) provides error() method use Template::Base; use base qw( Template::Base );
sub paths { my $self = shift;
# ...some code...
if ($all_is_well) { return \@list_of_directories; } else { return $self->error("cannot generate INCLUDE_PATH...\n"); } }
1;
# tolerate Silly Billy's file system conventions my $provider = Template::Provider->new({ DELIMITER => '; ', INCLUDE_PATH => 'C:/HERE/NOW; D:/THERE/THEN', });
# better solution: install Linux! :-)
On Win32 systems, the default delimiter is a little more intelligent, splitting paths only on ':' characters that aren't followed by a '/'. This means that the following should work as planned, splitting the INCLUDE_PATH into 2 separate directories, C:/foo and C:/bar.
# on Win32 only my $provider = Template::Provider->new({ INCLUDE_PATH => 'C:/Foo:C:/Bar' });
However, if you're using Win32 then it's recommended that you explicitly set the DELIMITER character to something else (e.g. ';') rather than rely on this subtle magic.
my $provider = Template::Provider->new({ ABSOLUTE => 1, });
# this is why it's disabled by default [% INSERT /etc/passwd %]
On Win32 systems, the regular expression for matching absolute pathnames is tweaked slightly to also detect filenames that start with a driver letter and colon, such as:
C:/Foo/Bar
my $provider = Template::Provider->new({ RELATIVE => 1, });
[% INCLUDE ../logs/error.log %]
my $provider = Template::Provider->new({ DEFAULT => 'notfound.html', });
If a non-existant template is requested through the Template process()
method, or by an INCLUDE, PROCESS or WRAPPER directive, then the
DEFAULT template will instead be processed, if defined. Note that the
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.
By default, the CACHE_SIZE is undefined and all compiled templates are cached. When set to any positive value, the cache will be limited to storing no more than that number of compiled templates. When a new template is loaded and compiled and the cache is full (i.e. the number of entries == CACHE_SIZE), the least recently used compiled template is discarded to make room for the new one.
The CACHE_SIZE can be set to 0 to disable caching altogether.
my $provider = Template::Provider->new({ CACHE_SIZE => 64, # only cache 64 compiled templates });
my $provider = Template::Provider->new({ CACHE_SIZE => 0, # don't cache any compiled templates });
my $provider = Template::Provider->new({ COMPILE_EXT => '.ttc', });
If COMPILE_EXT is defined (and COMPILE_DIR isn't, see below) then compiled template files with the COMPILE_EXT extension will be written to the same directory from which the source template files were loaded.
Compiling and subsequent reuse of templates happens automatically whenever the COMPILE_EXT or COMPILE_DIR options are set. The Template Toolkit will automatically reload and reuse compiled files when it finds them on disk. If the corresponding source file has been modified since the compiled version as written, then it will load and re-compile the source and write a new compiled version to disk.
This form of cache persistence offers significant benefits in terms of time and resources required to reload templates. Compiled templates can be reloaded by a simple call to Perl's require(), leaving Perl to handle all the parsing and compilation. This is a Good Thing.
my $provider = Template::Provider->new({ COMPILE_DIR => '/tmp/ttc', });
The COMPILE_EXT option may also be specified to have a consistent file extension added to these files.
my $provider1 = Template::Provider->new({ COMPILE_DIR => '/tmp/ttc', COMPILE_EXT => '.ttc1', });
my $provider2 = Template::Provider->new({ COMPILE_DIR => '/tmp/ttc', COMPILE_EXT => '.ttc2', });
When COMPILE_EXT is undefined, the compiled template files have the same name as the original template files, but reside in a different directory tree.
Each directory in the INCLUDE_PATH is replicated in full beneath the COMPILE_DIR directory. This example:
my $provider = Template::Provider->new({ COMPILE_DIR => '/tmp/ttc', INCLUDE_PATH => '/home/abw/templates:/usr/share/templates', });
would create the following directory structure:
/tmp/ttc/home/abw/templates/ /tmp/ttc/usr/share/templates/
Files loaded from different INCLUDE_PATH directories will have their compiled forms save in the relevant COMPILE_DIR directory.
On Win32 platforms a filename may by prefixed by a drive letter and colon. e.g.
C:/My Templates/header
The colon will be silently stripped from the filename when it is added
to the COMPILE_DIR value(s)
to prevent illegal filename being generated.
Any colon in COMPILE_DIR elements will be left intact. For example:
# Win32 only my $provider = Template::Provider->new({ DELIMITER => ';', COMPILE_DIR => 'C:/TT2/Cache', INCLUDE_PATH => 'C:/TT2/Templates;D:/My Templates', });
This would create the following cache directories:
C:/TT2/Cache/C/TT2/Templates C:/TT2/Cache/D/My Templates
my $provider = Template::Provider->new({ PARSER => MyOrg::Template::Parser->new({ ... }), });
use Template::Constants qw( :debug );
my $template = Template->new({ DEBUG => DEBUG_PROVIDER, });
fetch($name)
Returns a compiled template for the name specified. If the template cannot be found then (undef, STATUS_DECLINED) is returned. If an error occurs (e.g. read error, parse error) then ($error, STATUS_ERROR) is returned, where $error is the error message generated. If the TOLERANT flag is set the the method returns (undef, STATUS_DECLINED) instead of returning an error.
Stores the compiled template, $template, in the cache under the name,
$name. Susbequent calls to fetch($name)
will return this template in
preference to any disk-based file.
include_path(\@newpath))
Accessor method for the INCLUDE_PATH setting. If called with an argument, this method will replace the existing INCLUDE_PATH with the new value.
paths()
This method generates a copy of the INCLUDE_PATH list. Any elements in the
list which are dynamic generators (e.g. references to subroutines or objects
implementing a paths()
method) will be called and the list of directories
returned merged into the output list.
It is possible to provide a generator which returns itself, thus sending
this method into an infinite loop. To detect and prevent this from happening,
the $MAX_DIRS
package variable, set to 64 by default, limits the maximum
number of paths that can be added to, or generated for the output list. If
this number is exceeded then the method will immediately return an error
reporting as much.
Andy Wardley <abw@andywardley.com>
http://www.andywardley.com/|http://www.andywardley.com/
2.70, distributed as part of the Template Toolkit version 2.10, released on 24 July 2003.
Copyright (C) 1996-2003 Andy Wardley. All Rights Reserved. Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Template, Template::Parser, Template::Context
Template::Provider - Provider module for loading/compiling templates |