diff --git a/lib/WebGUI/Asset/Template.pm b/lib/WebGUI/Asset/Template.pm index f50300ce0..13e480fe0 100644 --- a/lib/WebGUI/Asset/Template.pm +++ b/lib/WebGUI/Asset/Template.pm @@ -135,6 +135,30 @@ Provides a mechanism to provide a templating system in WebGUI. =head1 ATTRIBUTES +#---------------------------------------------------------------------------- + +=head2 forms + +A hash of forms to be included in this template. The forms' template variables +will be automatically added to the L hash when the template is processed. + +Hash keys are the form's unique name, which will be prefixed to the form's +template variables + +=cut + +has forms => ( + traits => ['Hash'], + is => 'rw', + isa => 'HashRef', + default => sub { {} }, + handles => { + addForm => 'set', + getForm => 'get', + deleteForm => 'delete', + hasForms => 'count', + }, +); #---------------------------------------------------------------------------- @@ -150,7 +174,7 @@ Use L method to set parameters. has param => ( traits => [ 'Hash' ], - is => 'ro', + is => 'rw', isa => 'HashRef', default => sub { {} }, handles => { @@ -676,6 +700,8 @@ Will also process the style template attached to this template A hash reference containing template variables and loops. Automatically includes the entire WebGUI session. +These parameters will override any parameters set by L and L + =cut sub process { @@ -696,11 +722,20 @@ sub process { return $session->isAdminOn ? $i18n->get('template in clipboard') : ''; } + # Merge the forms with the prepared vars + if ( $self->hasForms ) { + for my $name ( keys %{$self->forms} ) { + my $form = $self->forms->{$name}; + $self->setParam( %{$form->toTemplateVars( "${name}_" )} ); + } + } + # 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' ); diff --git a/t/Asset/Template.t b/t/Asset/Template.t index 322a584b2..fda7b76ca 100644 --- a/t/Asset/Template.t +++ b/t/Asset/Template.t @@ -15,7 +15,7 @@ use WebGUI::Session; use WebGUI::Asset::Template; use Exception::Class; -use Test::More tests => 60; # increment this value for each test you create +use Test::More tests => 62; # increment this value for each test you create use Test::Deep; use Data::Dumper; use Test::Exception; @@ -75,6 +75,28 @@ $output = $template->process({}); ok( $output =~ m{^.+$}, 'style template is added' ); $template->style( undef ); +#----------------------------------------------------------------------------- +# Forms in templates +$template = WebGUI::Test->asset( + className => 'WebGUI::Asset::Template', + template => '', + namespace => 'WebGUI Test Template', + parser => $ht, + %tag, +); +my $form = WebGUI::FormBuilder->new( $session ); +$template->addForm( NAME => $form ); +$output = $template->process; +is( $output, $form->getHeader, 'form variables added to template' ); + +# Params passed into process() override everything +$output = $template->process({ NAME_header => 'NOT_SO_FAST' }); +is( $output, 'NOT_SO_FAST', "params passed into process() override all others" ); +$template->forms( {} ); +$template->param( {} ); + +#------------------------------------------------------------------------------ +# JSON output # See if template listens the Accept header $session->request->header('Accept' => 'application/json');