Template::Provider(3)

NAME
Template::Provider - Provider module for loading/compiling templates

SYNOPSIS
    $provider = Template::Provider->new(\%options);

    ($template, $error) = $provider->fetch($name);

DESCRIPTION
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 request, giving subsequent providers a chance to do so.

The Template::Provider can also be subclassed to provide templates from a different source, e.g. a database. See SUBCLASSING below.

This documentation needs work.

PUBLIC METHODS

  new(\%options)
Constructor method which instantiates and returns a new "Template::Provider" object. A reference to a hash array of configuration options may be passed.

See "CONFIGURATION OPTIONS" below for a summary of configuration options and Template::Manual::Config for full details.

  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 option is set the the method returns "(undef, STATUS_DECLINED)" instead of returning an error.

  load($name)
Loads a template without parsing or compiling it. This is used by the the INSERT directive.

  store($name, $template)
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.

CONFIGURATION OPTIONS
The following list summarises the configuration options that can be provided to the "Template::Provider" new() constructor. Please consult Template::Manual::Config for further details and examples of each configuration option in use.

  INCLUDE_PATH
The 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' ],
    });

  ABSOLUTE
The ABSOLUTE flag is used to indicate if templates specified with absolute filenames (e.g. '"/foo/bar"') should be processed. It is disabled by default and any attempt to load a template by such a name will cause a '"file"' exception to be raised.

    my $provider = Template::Provider->new({
        ABSOLUTE => 1,
    });

  RELATIVE
The RELATIVE flag is used to indicate if templates specified with filenames relative to the current directory (e.g. "./foo/bar" or "../../some/where/else") should be loaded. It is also disabled by default, and will raise a "file" error if such template names are encountered.

    my $provider = Template::Provider->new({
        RELATIVE => 1,
    });

  DEFAULT
The DEFAULT option can be used to specify a default template which should be used whenever a specified template can't be found in the INCLUDE_PATH.

    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.

  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 Encode for further information.

  CACHE_SIZE
The CACHE_SIZE option can be used to limit the number of compiled templates that the module should cache. By default, the CACHE_SIZE is undefined and all compiled templates are cached.

    my $provider = Template::Provider->new({
        CACHE_SIZE => 64,   # only cache 64 compiled templates
    });

  STAT_TTL
The STAT_TTL value can be set to control how long the "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
    });

  COMPILE_EXT
The 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',
    });

  COMPILE_DIR
The 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',
    });

  TOLERANT
The TOLERANT flag can be set to indicate that the "Template::Provider" module should ignore any errors encountered while loading a template and instead return "STATUS_DECLINED".

  PARSER
The PARSER option can be used to define a parser module other than the default of Template::Parser.

    my $provider = Template::Provider->new({
        PARSER => MyOrg::Template::Parser->new({ ... }),
    });

  DEBUG
The DEBUG option can be used to enable debugging messages from the Template::Provider module by setting it to include the "DEBUG_PROVIDER" value.

    use Template::Constants qw( :debug );

    my $template = Template->new({
        DEBUG => DEBUG_PROVIDER,
    });

SUBCLASSING
The "Template::Provider" module can be subclassed to provide templates from a different source (e.g. a database). In most cases you'll just need to provide custom implementations of the _template_modified() and _template_content() methods. If your provider requires and custom initialisation then you'll also need to implement a new _init() method.

Caching in memory and on disk will still be applied (if enabled) when overriding these methods.

  _template_modified($path)
Returns a timestamp of the $path passed in by calling stat(). This can be overridden, for example, to return a last modified value from a database. The value returned should be a timestamp value (as returned by time(), although a sequence number should work as well.

  _template_content($path)
This method returns the content of the template for all "INCLUDE", "PROCESS", and "INSERT" directives.

When called in scalar context, the method returns the content of the template located at $path, or "undef" if $path is not found.

When called in list context it returns "($content, $error, $mtime)", where $content is the template content, $error is an error string (e.g. ""$path: File not found""), and $mtime is the template modification time.

AUTHOR
Andy Wardley <abw@wardley.org> <http://wardley.org/>

COPYRIGHT
Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO
Template, Template::Parser, Template::Context