CGI::FormBuilder is a cool if sprawling module to
automatically build HTML forms with sticky widgets,
JS & Perl validation.
I love the idea, but the interface is a bit too
dictatorial for my liking. But while I wait for
a more modular, pluggable version, here's is a
horrible little hack to gather the bits that
would normally get passed to TT2.
package AHP::FakeTemplate;
=head1 DESCRIPTION
Tames CGI::FormBuilder.
The class pretends to be a Template in order to catch the
CGI::FormBuilder vars passed to process so that we can
use them outside of the callback.
=head1 SYNPOSIS
my $fb_vars;
my $form = CGI::FormBuilder->new(
...
template => {
engine => AHP::FakeTemplate->new_fake(\$fb_vars),
type => 'TT2',
template => 'ignored',
},
);
$form->render();
...
$template->process('my.tt2', { form => $fb_vars, ... });
my.tt2:
[% form.start %]
[% FOREACH f = form.fields %]
[%# "<pre>"; Dumper.dump(f) | html ; "</pre>" %]
[% f.required ? "<b>$f.label</b>" : f.label %]
[% f.field %] [% f.comment %]<br>
[% IF f.invalid %] Missing or invalid entry. [% END %]
<br>
[% END %]
[% form.submit %] <br>
[% form.end %]
=cut
use Carp ();
use Template ();
@ISA = ('Template'); # It's not really, but C::FB checks
# I'd prefer we couldn't act like one
use strict;
sub new { Carp::croak "$_[0] is not really a Template"; }
sub new_fake {
my ($class, $target_ref) = @_;
return bless $target_ref, $class;
}
# This sets the reference passed into new_fake
sub process {
my ($target_ref, $tmpl_ignored, $tt2vars, $tt2output_ref) = @_;
$$target_ref = $tt2vars;
$$tt2output_ref = '';
return 1;
}
1;