Looking to hire a Perl programmer?

Serving Catalyst::View::TT templates from a DBIC model

Following yesterday’s release of Template::Provider::DBIC I wanted to write a quick tutorial describing how to deploy it in your Catalyst projects.

Create your schema

The minimal DBIx::Class::Schema required to store your templates is as follows…

package MyApp::Schema::Template;
use base qw/ DBIx::Class /;

__PACKAGE__->load_components( 'PK::Auto', 'Core' );

__PACKAGE__->table('template');
__PACKAGE__->add_columns(
    'name',     { data_type = 'VARCHAR', size => 4 },
    'modified', { data_type = 'DATETIME'           },
    'content',  { data_type = 'TEXT'               },
);

__PACKAGE__->set_primary_key('name');
__PACKAGE__->add_unique_constraint( 'name', ['name'] );


1;

Create your view

The Template Toolkit lets you specify providers through the LOAD_TEMPLATES configuration option…

package MyApp::View::TT;

use base qw/ Catalyst::View::TT /;
use Template::Provider::DBIC::Schema;

__PACKAGE__->config({
    LOAD_TEMPLATES => [
        Template::Provider::DBIC::Schema->new({
            RESULTSET => MyApp->model('Template'),
        }),
    ],
});


1;

You’re now using DBIx::Class for your templates

Congratulations! The Template::Provider::DBIC documentation contains further details about configuration options, use with other providers, and serving templates from multiple ResultSets.

Back to UK Perl Programmer.

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