Subject: | Improve ease of use |
There are a few things which both Catalyst::View::TT and
Catalyst::View::TT::Alloy do which make things easy. First is using the
Catalyst configuration mechanism.
This allows me to easily have a different configuration in one or more
development environments and production. Particularly important for the
cache_dir parameter.
To set the ::TT and ::TT::Alloy equivalent of INCLUDE_PATHS[] I find
myself doing this:
before _build_xslate => sub {
my ( $self, $c ) = @_;
$self->log->error('START');
my $path = $self->path || [];
push( @{$path}, $c->path_to('root') );
push( @{$path}, $c->path_to( q{..}, 'SK', 'root' ) );
$self->path($path);
$self->log->error('END');
return;
};
To configure a different cache_dir in production and development
environments, I find myself manually setting a configuration variable
and setting $self->cache_dir() in a similar function.
Another useful helper function with be something that prepares a set of
Template Toolkit plugins for use.
This bit of code in my Xslate view takes a set of plugins string names,
loads them during the first templating call, caches the instances of
them for future calls, and injects them into the namespace. Quite useful
as a TT2 compatibility item. Again, would be nice if I could simply
define a 'TT::Xslate' => {PLUGIN => ['Plugin1', 'Plugin2']} section
within my standard Catalyst yml file without writing code in my
extension of View::Xslate.
has plugins => (is => 'rw', isa => 'ArrayRef[Str]', default => sub
{[qw{Template::Plugin::Date}]});
has plugin_cache => (is => 'rw', isa => 'HashRef[Template::Plugin]',
lazy_build => 1);
sub _build_plugin_cache {
my ( $self ) = @_;
$self->log->error('START');
my %plugins;
for my $plug (@{$self->plugins}) {
my $varName;
if ($plug =~ m/([^:]+)$/) {
$varName = $1;
}
Class::MOP::load_class($plug);
$plugins{$varName} = $plug->new();
}
$self->log->error('END');
return \%plugins;
}
before render => sub {
my ( $self, $c ) = @_;
$self->log->error('START');
# Merge plugin namespace
$c->stash(%{$self->plugin_cache});
$self->log->error('END');
return;
};