NAME

Template::Provider::DBI - A class to allow retrieval of templates from a DB

VERSION

This documentation refers to version 0.01.

SYNOPSIS

use Template::Provider::DBI;
use DBI;

my $dbh = DBI->connect('dbi:SQLite:./mydatabase.db');
my $dbi = Template::Provider::DBI->new({ DBI_DBH => $dbh });

my $tt = Template->new({ LOAD_TEMPLATES => [ $dbi ] });
$tt->process('mytemplate.tt', \%vars);

DESCRIPTION

This class adds a provider to Template Toolkit to retrieve templates from a database of your choice. Using the LOAD_TEMPLATES option to Template, multiple providers can be created and used. The DBI provider searches for the given template name, and returns DECLINED when it can't find it, to allow other providers to be checked.

Caching

Caching is supported if DateTime::Format::DBI supports your database. The DateTime formatter/parser is used to convert timestamps out of the database into epoch times for Template Toolkit. Caching is done through the usual Template Provider method of storing the compiled template in a file.

Usage

To use this module, create an instance of it (see new below), and pass it to Template Toolkit's LOAD_TEMPLATES option. If you want to use other template providers as well (even the default file template), then you need to also create instances of them, and pass them to LOAD_TEMPLATES in the order you would like them to be checked.

SUBROUTINES/METHODS

new (constructor)

Parameters: \%options

Many options are supported, most have defaults:

DBI_DBH

A DBI database handle object, as returned by DBI->connect. This argument is optional if you are providing a DBI_DSN argument.

DBI_DSN

A database source name, to be passed to DBI->connect. This will be used to make and store a local database handle. It is optional if a DBI_DBH is provided, if both are provided, an error is thrown.

DBI_TABLE

The name of the database table containing your templates. This will default to 'templates' if not provided.

DBI_TMPLFIELD

The name of the table field containing the actual template data. This will default to 'template' if not provided.

DBI_FILEFIELD

The name of the table field containing the template filename. This will default to 'filename'.

DBI_MODIFIEDFIELD

The name of the table field containing the timestamp or datetime of when the template data was last modified. Defaults to 'modified' if not provided.

DBI_DT

If DateTime::Format::DBI does not support your database, then you can try getting around it by providing an object in the DBI_DT option that has a "parse_timestamp" method, which will be passed the contents of your DBI_MODIFIEDFIELD.

DBI_QUERY

The query used to retrieve the template data from the database will be create simply as:

SELECT $self->{DBI_TMPLFIELD}, $self->{DBI_MODIFIEDFIELD}$modified FROM $self->{DBI_TABLE} WHERE $self->{DBI_FILEFIELD} = ?;

If you need a more complex query then provide it here, it should return at least the template data field first, then the modify date field next (or NULL), in that order.

DEPENDENCIES

Modules used, version dependencies, core yes/no

DBI

DateTime::Format::DBI

NOTES

DateTime::Format::DBI produces DBI warnings when used with SQLite. It calls DBI::_dbtype_names($dbh); .. Not my fault, honest!

BUGS AND LIMITATIONS

None known currently, please email the author if you find any.

AUTHOR

Jess Robinson <cpan@desert-island.demon.co.uk>