########################################################################### # Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved. # Please visit http://formbuilder.org for tutorials, support, and examples. ########################################################################### package CGI::FormBuilder::Template::CGI_SSI; =head1 NAME CGI::FormBuilder::Template::CGI_SSI - FormBuilder interface to CGI::SSI =head1 SYNOPSIS my $form = CGI::FormBuilder->new( fields => \@fields, template => { type => 'CGI_SSI', file => "template.html", }, ); =cut use Carp; use strict; use warnings; no warnings 'uninitialized'; use CGI::FormBuilder::Util; use CGI::SSI; use base 'CGI::SSI'; our $VERSION = '3.20'; # # For legacy reasons, and due to its somewhat odd interface, # CGI::SSI vars use a completely different naming scheme. # our %FORM_VARS = ( 'js-head' => 'jshead', 'form-title' => 'title', 'form-start' => 'start', 'form-submit' => 'submit', 'form-reset' => 'reset', 'form-end' => 'end', 'form-invalid' => 'invalid', 'form-required' => 'required', ); our %FIELD_VARS = map { $_ => "$_-%s" } qw( field value label type comment required error invalid missing nameopts cleanopts ); sub new { my $self = shift; my $class = ref($self) || $self; my $opt = arghash(@_); $opt->{die_on_bad_params} = 0; # force to avoid blow-ups my %opt2 = %$opt; delete $opt2{virtual}; delete $opt2{file}; delete $opt2{string}; $opt->{engine} = CGI::SSI->new(%opt2); return bless $opt, $class; # rebless } sub engine { return shift()->{engine}; } sub render { my $self = shift; my $tvar = shift || puke "Missing template expansion hashref (\$form->prepare failed?)"; while(my($to, $from) = each %FORM_VARS) { debug 1, "renaming attr $from to: " # my @fieldlist; for my $field (@{$tvar->{fields}}) { # Field name is usually a good idea my $name = $field->{name}; debug 1, "expanding field: $name"; # Get all values my @value = @{$tvar->{field}{$name}{values} || []}; my @options = @{$tvar->{field}{$name}{options} || []}; # # Auto-expand all of our field tags, such as field, label, value # comment, error, etc, etc # my %all_loop; while(my($key, $str) = each %FIELD_VARS) { my $var = sprintf $str, $name; $all_loop{$key} = $tvar->{field}{$name}{$key}; $tvar->{$var} = "$tvar->{field}{$name}{$key}"; # fuck Perl debug 2, " >> of the same name prefixed with "field-" in the template. So, if you defined a field called "email", then you would setup a variable called C<< >> in your template. In addition, there are a couple special fields: - JavaScript to stick in - The of the HTML form <!--#echo var="form-start" --> - Opening <form> tag and internal fields <!--#echo var="form-submit" --> - The submit button(s) <!--#echo var="form-reset" --> - The reset button <!--#echo var="form-end" --> - Just the closing </form> tag Let's look at an example C<form.html> template we could use: <html> <head> <title>User Information

User Information

Please fill out the following information: 's corresponds to a field -->

Your full name:

Your email address:

Choose a password:

Please confirm it:

Your home zipcode:

As you see, you get a C<< >> for each for field you define. However, you may want even more control. That is, maybe you want to specify every nitty-gritty detail of your input fields, and just want this module to take care of the statefulness of the values. This is no problem, since this module also provides several other C<< >> tags as well: - The value of a given field - The human-readable label - Any optional comment - Error text if validation fails - See if the field is required This means you could say something like this in your template: : "> And B would take care of the value stickiness for you, while you have control over the specifics of the C<< >> tag. A sample expansion may create HTML like the following: Email: You must enter a valid value Note, though, that this will only get the I value in the case of a multi-value parameter (for example, a multi-select list). Multiple values (loops) in C<< CGI_SSI >> are not yet implemented. For more information on templates, see L. =head1 SEE ALSO L, L, L =head1 REVISION $Id: HTML.pm 97 2007-02-06 17:10:39Z nwiger $ =head1 AUTHOR Copyright (c) L. 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. =cut