From 13e80401909f3ad7452930292978303f22e1c862 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Thu, 7 Apr 2011 20:46:21 -0500 Subject: [PATCH] add setParam, getParam, deleteParam to template assets --- lib/WebGUI/Asset/Template.pm | 43 ++++++++++++++++++++++++++++++++---- t/Asset/Template.t | 13 ++++++++++- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/lib/WebGUI/Asset/Template.pm b/lib/WebGUI/Asset/Template.pm index 3023e6959..fbd603a19 100644 --- a/lib/WebGUI/Asset/Template.pm +++ b/lib/WebGUI/Asset/Template.pm @@ -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 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 diff --git a/t/Asset/Template.t b/t/Asset/Template.t index 6fd492e41..66f7072b6 100644 --- a/t/Asset/Template.t +++ b/t/Asset/Template.t @@ -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 = " true XY "; +my $tmplText = " true XY "; 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');