webgui/lib/WebGUI/Asset/Template/TemplateToolkit.pm
2011-12-28 11:30:38 -08:00

116 lines
3.7 KiB
Perl

package WebGUI::Asset::Template::TemplateToolkit;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2012 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use base 'WebGUI::Asset::Template::Parser';
use Template;
use WebGUI::Template::Provider;
#-------------------------------------------------------------------
sub _rewriteVars { # replace dots with underscrores in keys (except in keys that aren't usable as variables (URLs etc.))
my $vars = shift;
my $newVars = {};
foreach my $key (keys %$vars){
my $newKey = $key;
$newKey =~ s/\./_/g if $newKey !~ /\//;
if ( ref $vars->{$key} eq 'ARRAY') {
foreach my $entry (@{$vars->{$key}}) {
if ( ref $entry eq 'HASH' ) {
push(@{$newVars->{$newKey}}, _rewriteVars($entry));
}
else {
push(@{$newVars->{$newKey}}, $entry );
}
}
} elsif(ref $vars->{$key} eq 'HASH') {
$newVars->{$newKey} = _rewriteVars($vars->{$key});
} else {
$newVars->{$newKey} = $vars->{$key};
}
}
return $newVars;
}
#-------------------------------------------------------------------
=head2 getName ( )
Returns the human readable name of this parser.
=cut
sub getName {
my $self = shift;
return "Template Toolkit";
}
#-------------------------------------------------------------------
=head2 process ( template, vars )
Evaluate a template replacing template commands for HTML.
=head3 template
A scalar variable containing the template.
=head3 vars
A hash reference containing template variables and loops.
=cut
# TODO: Have this throw an error so we can catch it and print more information
# about the template that has the error. Finding an "ERROR: Error in template"
# in the error log is not very helpful...
sub process {
my $self = shift;
my $template = shift;
my $vars = $self->addSessionVars(shift);
my ($t,$output);
eval {
my $config = $self->session->config->get( 'template' ) || {};
$config->{INTERPOLATE} //= 1; # expand "$var" in plain text
$config->{POST_CHOMP} //= 1; # cleanup whitespace
$config->{EVAL_PERL} //= 0; # evaluate Perl code blocks
# Add WebGUI::Template::Plugin to PLUGIN_BASE
if ( defined $config->{PLUGIN_BASE} && !ref $config->{PLUGIN_BASE} ) {
$config->{PLUGIN_BASE} = [ $config->{PLUGIN_BASE} ];
}
elsif ( !defined $config->{PLUGIN_BASE} ) {
$config->{PLUGIN_BASE} = [];
}
push @{$config->{PLUGIN_BASE}}, 'WebGUI::Template::Plugin';
# Allow WebGUI assets to be included in templates
$config->{LOAD_TEMPLATES} = [ WebGUI::Template::Provider->new( $self->session, $config ) ];
$t = Template->new( $config );
$vars = _rewriteVars($vars);
$vars->{_session} = $self->session;
unless ($t->process( \$template, $vars, \$output)) {
my $e = $t->error;
$self->session->log->error($e);
die $e;
}
};
if ($@) {
WebGUI::Error::Template->throw( error => $@ );
}
return $output;
}
1;