|
::Features
::Download
::Tutorial
::Examples
::Documentation ::Join the Mailing List ::List Archives ::Contribute |
|
NAMECGI::FormBuilder::Template - Template adapters for FormBuilder
SYNOPSIS
# Define a template engine
package CGI::FormBuilder::Template::Whatever;
use base 'Whatever::Template::Module';
sub new {
my $self = shift;
my $class = ref($self) || $self;
my %opt = @_;
# override some options
$opt{some_setting} = 0;
$opt{another_var} = 'Some Value';
# instantiate the template engine
$opt{engine} = Whatever::Template::Module->new(%opt);
return bless \%opt, $class;
}
sub render {
my $self = shift;
my $form = shift; # only arg is form object
# grab any manually-set template params
my %tmplvar = $form->tmpl_param;
# example template manipulation
my $html = $self->{engine}->do_template(%tmplvar);
return $html; # scalar HTML is returned
}
DESCRIPTIONThis documentation describes the usage of FormBuilder templates, as well as how to write your own template adapter. The template engines serve as adapters between CPAN template modules
and FormBuilder. A template engine is invoked by using the
my $form = CGI::FormBuilder->new(
template => 'filename.tmpl'
);
This example points to a filename that contains an For example, you could turn on caching in
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
filename => 'form.tmpl',
shared_cache => 1
}
);
As mentioned, specifying a hashref allows you to use an alternate template
processing system like the
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'TT2', # use Template Toolkit
template => 'form.tmpl',
},
);
The
Builtin - Included, default rendering if no template specified
Div - Render form using <div> (no tables)
HTML - HTML::Template
Text - Text::Template
TT2 - Template Toolkit
Fast - CGI::FastTemplate
In addition to one of these types, you can also specify a complete package name,
in which case that module will be autoloaded and its
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'My::Template::Module',
template => 'form.tmpl',
},
);
All other options besides
SUBCLASSING TEMPLATE ADAPTERSIn addition to the above included template engines, it is also possible to write your own rendering module. If you come up with something cool, please let the mailing list know! To do so, you need to write a module which has a sub called This is actually not hard. Here's a simple adapter which would manipulate
an
# This file is My/HTML/Template.pm
package My::HTML::Template;
use CGI::FormBuilder::Template::HTML;
use base 'CGI::FormBuilder::Template::HTML';
sub render {
my $self = shift; # class object
my $form = shift; # $form as only argument
# the template object (engine) lives here
my $tmpl = $self->engine;
# setup vars for our fields (objects)
for ($form->field) {
$tmpl->param($_ => $_->value);
}
# render output
my $html = $tmpl->output;
# return scalar;
return $html;
}
1; # close module
Then in FormBuilder:
use CGI::FormBuilder;
use My::HTML::Template; # your module
my $tmpl = My::HTML::Template->new;
my $form = CGI::FormBuilder->new(
fields => [qw(name email)],
header => 1,
template => $tmpl # pass template object
);
# set our company from an extra CGI param
my $co = $form->cgi_param('company');
$tmpl->engine->param(company => $co);
# and render like normal
print $form->render;
That's it! For more details, the best thing to do is look through the guts of one of the existing template engines and go from there.
SEE ALSOthe CGI::FormBuilder manpage, the CGI::FormBuilder::Template::HTML manpage, the CGI::FormBuilder::Template::Text manpage, the CGI::FormBuilder::Template::TT2 manpage, the CGI::FormBuilder::Template::Fast manpage
REVISION$Id: Template.pm 100 2007-03-02 18:13:13Z nwiger $
AUTHORCopyright (c) 2000-2006 Nate Wiger <nate@wiger.org>. All Rights Reserved. This module is free software; you may copy this under the terms of the GNU General Public License, or the Artistic License, copies of which should have accompanied your Perl kit. |
| FormBuilder is © 2000-2006 Nate Wiger, with contributions from many people. |