add setParam, getParam, deleteParam to template assets

This commit is contained in:
Doug Bell 2011-04-07 20:46:21 -05:00
parent 5f66eaac45
commit 13e8040190
2 changed files with 51 additions and 5 deletions

View file

@ -127,7 +127,37 @@ Provides a mechanism to provide a templating system in WebGUI.
=head1 SYNOPSIS
use WebGUI::Asset::Template;
my $template = WebGUI::Asset::Template->newById( $session, "template id" );
$template->setParam( param => "value", param2 => "value" );
print $template->process;
=head1 ATTRIBUTES
#----------------------------------------------------------------------------
=head2 param
Save params in the template for later processing. This allows a template to be
passed around, adding variables until finally it is processed and output for
the user.
Use L<setParam> method to set parameters.
=cut
has param => (
traits => [ 'Hash' ],
is => 'ro',
isa => 'HashRef',
default => sub { {} },
handles => {
setParam => 'set',
getParam => 'get',
deleteParam => 'delete',
},
);
=head1 METHODS
@ -573,10 +603,15 @@ sub process {
return $session->isAdminOn ? $i18n->get('template in clipboard') : '';
}
# Merge the passed-in vars with the prepared vars
if ( keys %$vars > 0 ) { # can't call setParam with an empty hash
$self->setParam( %$vars );
}
# Return a JSONinfied version of vars if JSON is the only requested content type.
if ( defined $session->request && $session->request->header('Accept') eq 'application/json' ) {
$session->response->content_type( 'application/json' );
return to_json( $vars );
return to_json( $self->param );
}
$self->prepare unless ($self->{_prepared});
@ -586,7 +621,7 @@ sub process {
: $self->template
;
my $output;
eval { $output = $parser->process($template, $vars); };
eval { $output = $parser->process($template, $self->param); };
if (my $e = Exception::Class->caught) {
$session->log->error(sprintf "Error processing template: %s, %s, %s", $self->getUrl, $self->getId, $e->error);
my $i18n = WebGUI::International->new($session, 'Asset_Template');
@ -650,7 +685,7 @@ A scalar containing the template text.
=head3 vars
A hash reference containing template variables.
A hash reference containing template variables to add to the existing params.
=head3 parser

View file

@ -29,7 +29,7 @@ my %tag = ( tagId => $tag->getId, status => "pending" );
my $list = WebGUI::Asset::Template->getList($session);
cmp_deeply($list, {}, 'getList with no classname returns an empty hashref');
my $tmplText = " <tmpl_var variable> <tmpl_if conditional>true</tmpl_if> <tmpl_loop loop>XY</tmpl_loop> ";
my $tmplText = " <tmpl_var variable> <tmpl_if conditional>true</tmpl_if> <tmpl_loop loop>XY</tmpl_loop> <tmpl_var setParam_var> ";
my %var = (
variable=>"AAAAA",
conditional=>1,
@ -47,10 +47,13 @@ isa_ok($template, 'WebGUI::Asset::Template', "creating a template");
is($template->get('parser'), 'WebGUI::Asset::Template::HTMLTemplate', 'default parser is HTMLTemplate');
$var{variable} = "BBBBB";
$template->setParam( setParam_var => 'HUEG SUCCESS' );
$output = $template->process(\%var);
ok($output =~ m/\bBBBBB\b/, "process() - variables");
ok($output =~ m/true/, "process() - conditionals");
ok($output =~ m/\b(?:XY){5}\b/, "process() - loops");
ok($output =~ m/\bHUEG SUCCESS\b/, "process() merges with setParam" );
$template->deleteParam( 'setParam_var' );
# See if template listens the Accept header
$session->request->header('Accept' => 'application/json');
@ -60,6 +63,14 @@ my $andNowItsAPerlHashRef = eval { from_json( $json ) };
ok( !$@, 'Accept = json, JSON is returned' );
cmp_deeply( \%var, $andNowItsAPerlHashRef, 'Accept = json, The correct JSON is returned' );
# Try Accept application/json again, but with a setParam
$template->setParam( herp_status => 'derp' );
$json = $template->process(\%var);
$andNowItsAPerlHashRef = eval { from_json( $json ) };
ok( !$@, 'Accept = json, JSON is returned with setParam' );
# Also test getParam
cmp_deeply( { %var, herp_status => $template->getParam('herp_status') }, $andNowItsAPerlHashRef, 'Accept = json, The correct JSON is returned with setParam' );
# Done, so remove the json Accept header.
$session->request->headers->remove_header('Accept');