Looking to hire a Perl programmer?
You can download Template::Provider::DBIC from CPAN or find it in the DBIx::Class subversion repository.
This document describes Template::Provider::DBIC version 0.01
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.
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.
This provider requires a schema containing at least the following:
$template->provider($name)
is called the provider will search this column for the corresponding $name
. For this reason the column must be a unique key, else an exception will be raised.
In addition to supplying a RESULTSET or SCHEMA and the standard Template::Provider options, you may set the following preferences:
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,
}),
],
});
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.
This method is called automatically during Template's ->process()
and returns a compiled template for the given $name
, using the cache where possible.
In addition to errors raised by Template::Provider and DBIx::Class, Template::Provider::DBIC may generate the following error messages:
->process()
must start with the name of the result set to search in.
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.
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.
You can find documentation for this module with the perldoc command.
perldoc Template::Provider::DBIC
You may also look for information at:
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.