Looking to hire a Perl programmer?

Template::Provider::DBIC - Load templates using DBIx::Class.

You can download Template::Provider::DBIC from CPAN or find it in the DBIx::Class subversion repository.

Version

This document describes Template::Provider::DBIC version 0.01

Synopsis

use My::DBIC::Schema;
use Template;
use Template::Provider::DBIC;

my $schema = My::DBIC::Schema->connect(
    $dsn, $user, $password, \%options
);
my $resultset = $schema->resultset('Template');

If all of your templates are stored in a single table the most convenient method is to pass the provider a DBIx::Class::ResultSet.

my $template = Template->new({
    LOAD_TEMPLATES => [
        Template::Provider::DBIC->new({
            RESULTSET => $resultset,
            # Other template options like
            # COMPILE_EXT...
        }),
    ],
});

# Process the template 'my_template' from the
# resultset 'Template'.
$template->process('my_template');

# Process the template 'other_template' from the
# resultset 'Template'.
$template->process('other_template');

Alternatively, where your templates are stored in several tables you can pass a DBIx::Class::Schema and specify the result set and template name in the form ResultSet/template_name.

my $template2 = Template->new({
    LOAD_TEMPLATES => [
        Template::Provider::DBIC->new({
            SCHEMA => $schema,
            # Other template options...
        }),
    ],
});

# Process the template 'my_template' from the
# resultset 'Template'.
$template->process('Template/my_template');

# Process the template 'my_template' from the
# resultset 'Other'.
$template>>process('Other/my_template');

In cases where both are supplied, the more specific RESULTSET will take precedence.

Description

Template::Provider::DBIC allows a Template object to fetch its data using DBIx::Class instead of, or in addition to, the default filesystem-based Template::Provider.

Schema

This provider requires a schema containing at least the following:

Options

In addition to supplying a RESULTSET or SCHEMA and the standard Template::Provider options, you may set the following preferences:

COLUMN_NAME
The table column that contains the template name. This will default to 'name'.
COLUMN_CONTENT
The table column that contains the template data itself. This will default to 'content'.
COLUMN_MODIFIED
The table column that contains the date that the template was last modified. This will default to 'modified'.

Use with other providers

By default Template::Provider::DBIC will raise an exception when it cannot find the named template. When TOLERANT is set to true it will defer processing to the next provider specified in LOAD_TEMPLATES where available. For example:

my $template = Template->new({
    LOAD_TEMPLATES => [
        Template::Provider::DBIC->new({
            RESULTSET => $resultset,
            TOLERANT  => 1,
        }),
        Template::Provider->new({
            INCLUDE_PATH => $path_to_templates,
        }),
    ],
});

Caching

When caching is enabled, by setting COMPILE_DIR and/or COMPILE_EXT, Template::Provider::DBIC will create a directory consisting of the database DSN and table name. This should prevent conflicts with other databases and providers.

Public Methods

->fetch( $name )

This method is called automatically during Template's ->process() and returns a compiled template for the given $name, using the cache where possible.

Diagnostics

In addition to errors raised by Template::Provider and DBIx::Class, Template::Provider::DBIC may generate the following error messages:

A valid DBIx::Class::Schema or ::ResultSet is required
One of the SCHEMA or RESULTSET configuration options must be provided.
%s not valid: must be of the form $table/$template
When using Template::Provider::DBIC with a DBIx::Class::Schema object, the template name passed to ->process() must start with the name of the result set to search in.
'%s' is not a valid result set for the given schema
Couldn't find the result set %s in the given DBIx::Class::Schema object.
Could not retrieve '%s' from the result set '%s'
Unless TOLERANT is set to true failure to find a template with the given name will raise an exception.

Dependencies

Template::Provider::DBIC requires the following modules:

Additionally, use of this module requires an object of the class DBIx::Class::Schema or DBIx::Class::ResultSet.

Bugs

Please report any bugs or feature requests to bug-template-provider-dbic at rt.cpan.org, or through the web interface at rt.cpan.org.

Support

You can find documentation for this module with the perldoc command.

perldoc Template::Provider::DBIC

You may also look for information at:

Author

Dave Cardwell

Copyright © 2007 Dave Cardwell. All rights reserved.

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

Back to UK Perl Programmer.

Dave Cardwell (G+ | Tw | LI | GH) :: Sitemap