[Catalyst] dynamic include path for tt

Ben Norman ben.norman at qmunity.net
Fri Nov 4 02:18:02 CET 2005


I would like to prefix the list of include paths for templates with a 
value based on a stash item which is derived for each call.
The following example did this by making available the Template Toolkits 
provider in a class variable so it can be used in the process function 
of a TT view component.
Is there another way to provide a dynamic include path for the template 
toolkit to a catalyst app? I'm not convinced my code is as clean as it 
could be.

package MyApp::V::TT;

use strict;
use base 'Catalyst::View::TT';
use Template::Constants qw( :debug );

__PACKAGE__->mk_classdata('provider');

sub new {
    my $self = shift;
    my $c = shift;
    $self = $self->Catalyst::Base::new(@_);
    $self->mk_classdata('provider');
    $self->provider(Template::Provider->new());
    my $root = $c->config->{root};
    $self->config( {
        LOAD_TEMPLATES  => [$self->provider],
        EVAL_PERL       => 0,
        INCLUDE_PATH    => [$root, "$root/base"],
        PRE_PROCESS     => 'macros.tt',
        TRIM            => 1,
#       DEBUG           => DEBUG_ALL,
        } );
#    if ( $c->debug && not exists $self->config->{CONTEXT} ) {
#       $self->config->{CONTEXT} = Template::Timer->new($self->config);
#    }
    $self->template( Template->new( $self->config ) );
    return $self;
}

sub process {
    my ($self, $c) = @_;
    my $root = $c->config->{root};
    my $tabpath = $root . '/' . $c->stash->{library_base};
    $self->provider->include_path( [ $tabpath, $root, 
"$root/../../templates", ] );
    $c->stash->{template} .= '.tt2';
    return $self->SUPER::process($c);
}

1;

Thanks
Ben




More information about the Catalyst mailing list