add forms to templates to work directly with FormBuilder objects
This commit is contained in:
parent
199e2240a7
commit
9a90ad6d2f
2 changed files with 59 additions and 2 deletions
|
|
@ -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<param> 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<setParam> 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<param> and L<forms>
|
||||
|
||||
=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' );
|
||||
|
|
|
|||
|
|
@ -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{^<IGOTSTYLE>.+</IGOTSTYLE>$}, 'style template is added' );
|
||||
$template->style( undef );
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Forms in templates
|
||||
$template = WebGUI::Test->asset(
|
||||
className => 'WebGUI::Asset::Template',
|
||||
template => '<tmpl_var NAME_header>',
|
||||
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');
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue