I've done a first cut at modifying CGI::FormBuilder to use ASP's $Response->Include()
as a templating mechanism.
In an ASP file your form is created:
<<<
<html>
<head>
<title>Try Formbuilder with ASP</title>
</head>
<body>
<h2>A really simple CGI::FormBuilder example using ASP</h2>
<!-- before ASP/CGI::FormBuilder stuff -->
<%
require CGI::FormBuilder;
my $form = CGI::FormBuilder->new
(
header => 0,
method => 'GET',
fields => [qw/email /],
javascript => 0,
reset => 0,
required => [qw/email /],
validate => { email => 'EMAIL' },
sticky => 1, # 0 or 1
);
$form->field ( name => 'email', validate => 'EMAIL', size => 25 );
if ( $form->submitted( ) && $form->validate( ) )
{
print $form->confirm( );
}
else
{
print $form->render
(
submit => 'Submit',
template => {
type => 'ASPInclude',
template => 'inc_show_params.asp',
response => $Response,
}
);
}
%>
>>>
Note, in particular, the $form->render() method at the bottom. I pass $Response as a
parameter as its a simple and effective way to ensure
that CGI::FormBuilder gets the appropriate $Response object (which may be in some odd
namespace).
The file inc_show_params.asp is a demo that just dumps the parameters:
<<<
<p>This is my asp file
<p>Below is a dump of all arguments:
<p><hr>
<p><pre>
<%
use Data::Dumper;
$Data::Dumper::Indent = 1;
my ( @args ) = @_;
print $Server->HTMLEncode( Data::Dumper->Dump([@args], [qw(*args)]));
%>
</pre></p><hr>
>>>
The code below is a more-or-less replacement using ASP for the default
CGI::FormBuilder style:
<<<
<p>This is a demo form - this content is in a separate ASP file included by
CGI::FormBuilder.
<p>Fields marked with "*" must be supplied.
<%
my ( $formRef ) = @_;
my ( $fieldsArrayRef ) = $formRef->{ 'fields' };
if ( $formRef->{'invalid'}) { %>
<p><b>There were <%= $formRef->{'invalid'} %> error<%= ( $formRef->{'invalid'} > 1 ?
's' : '' ) %> in your submission.</b></p>
<%
}
%>
<p>
<%= $formRef->{ 'start' } %>
<table border="1" cellpadding="5">
<%
foreach my $fieldRef ( @{$fieldsArrayRef}) {
%>
<tr>
<td>
<%= ( $fieldRef->{ 'invalid' } ? '<font color="red">' : '' ) %>
<%= $fieldRef->{ 'label' } %>
<%= ( $fieldRef->{ 'required' } ? '*' : '' ) %>
<%= ( $fieldRef->{ 'invalid' } ? '</font>' : '' ) %>
</td>
<td><%= $fieldRef->{ 'field' } %>
</td>
</tr>
<% } %>
<tr>
<td colspan="2" align="center">
<%= $formRef->{ 'reset' } %>
<%= $formRef->{ 'submit' } %>
</td>
</tr>
</table>
<%= $formRef->{ 'end' } %>
>>>
Phew... hope this post isn't too long!
To achieve all this was surprisingly easy:
> diff /usr/local/lib/site_perl/CGI/FormBuilder.pm
>/my/dodgy/CGI/Customised_FormBuilder.pm
953c955
< if (ref $args{template} eq 'HASH' && $args{template}{type} eq 'TT2') {
---
> if (ref $args{template} eq 'HASH' && $args{template}{type} =~
>m/^(TT2|ASPInclude)$/ ) { # added by Ellers
1178a1183,1207
>
> } elsif ($tmpltype eq 'ASPInclude') { # Added by Ellers
>
> eval { require Apache::ASP };
> puke "Can't use templates because the Apache::ASP is not installed!" if
>$@;
>
> my ( $aspFile, $aspResponse, $aspContentRef, $aspArgRef );
> $aspFile = $tmplopt{template}
> || puke "ASP Include not specified";
> $aspResponse = $tmplopt{response}
> || puke "ASP Response not specified";
>
> # special fields
> $tmplvar{'start'} = $formtag;
> $tmplvar{'submit'} = $submit;
> $tmplvar{'reset'} = $reset;
> $tmplvar{'end'} = '</form>';
> $tmplvar{'jshead'} = $jsfunc;
> $tmplvar{'invalid'} = $self->{state}{invalid};
> $tmplvar{'fields'} = [ map $tmplvar{field}{$_},
> @{ $self->{field_names} } ];
> $aspArgRef = \%tmplvar;
>
> $aspContentRef = $aspResponse->TrapInclude( $aspFile,
>$aspArgRef );
> $outhtml = $header . $$aspContentRef;
If that's a bit cumbersome, I've uploaded various files including my dodgy modified
FormBuilder.pm:
http://members.iinet.net.au/~ellers/cgi-formbuilder/
PLEASE NOTE:
1. I've done a few other changes to the above, such as spaces between multiple submit
buttons etc, but it should be ok...
2. The server above doesn't run Apache::ASP so best to save the files and run on your
own server
TODO:
- check templating work for $form->confirm()
- add overall doco, etc
It'd be great if anyone who is interested in this work could give it a go and send
comments through. If/when its reliable enough I can
submit it as a patch to the 'real' thing.
Ellers
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]