~ubuntu-branches/ubuntu/precise/libcgi-formbuilder-perl/precise

« back to all changes in this revision

Viewing changes to lib/CGI/FormBuilder/Template/CGI_SSI.pm

  • Committer: Package Import Robot
  • Author(s): Jonas Smedegaard
  • Date: 2011-09-11 02:55:17 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110911025517-nhr6j6fwwc9xc4l1
Tags: 3.06-1
* New upstream release.
* Use CDBS perl-makemaker.mk (not deprecated perlmodule.mk).
  Tighten build-dependency on CDBS.
* Update copyright file:
  + Fix use Comment field (not License-Comments).
  + Quote license names in comments.
* Add patch cherry-picked upstream to fix testsuite.
* Update package relations:
  + Build-depend on and suggest libcgi-ssi-perl.
  + Make suggestions unversioned and drop conflicts: satisfied even in
    oldstable.
  + Maintain all relations using CDBS in rules file.
* Stop preserving cruft no long shipped upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
###########################################################################
 
3
# Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved.
 
4
# Please visit http://formbuilder.org for tutorials, support, and examples.
 
5
###########################################################################
 
6
 
 
7
package CGI::FormBuilder::Template::CGI_SSI;
 
8
 
 
9
=head1 NAME
 
10
 
 
11
CGI::FormBuilder::Template::CGI_SSI - FormBuilder interface to CGI::SSI
 
12
 
 
13
=head1 SYNOPSIS
 
14
 
 
15
    my $form = CGI::FormBuilder->new(
 
16
                    fields   => \@fields,
 
17
                    template => {
 
18
                      type => 'CGI_SSI',
 
19
                      file => "template.html",
 
20
                    },
 
21
               );
 
22
 
 
23
=cut
 
24
 
 
25
use Carp;
 
26
use strict;
 
27
use warnings;
 
28
no  warnings 'uninitialized';
 
29
 
 
30
use CGI::FormBuilder::Util;
 
31
use CGI::SSI;
 
32
use base 'CGI::SSI';
 
33
 
 
34
our $REVISION = do { (my $r='$Revision: 97 $') =~ s/\D+//g; $r };
 
35
our $VERSION = '3.06';
 
36
 
 
37
#
 
38
# For legacy reasons, and due to its somewhat odd interface, 
 
39
# CGI::SSI vars use a completely different naming scheme.
 
40
#
 
41
our %FORM_VARS = (
 
42
    'js-head'       =>  'jshead',
 
43
    'form-title'    =>  'title',
 
44
    'form-start'    =>  'start',
 
45
    'form-submit'   =>  'submit',
 
46
    'form-reset'    =>  'reset',
 
47
    'form-end'      =>  'end',
 
48
    'form-invalid'  =>  'invalid',
 
49
    'form-required' =>  'required',
 
50
);
 
51
 
 
52
our %FIELD_VARS = map { $_ => "$_-%s" } qw(
 
53
    field
 
54
    value
 
55
    label
 
56
    type
 
57
    comment
 
58
    required
 
59
    error
 
60
    invalid 
 
61
    missing
 
62
    nameopts
 
63
    cleanopts
 
64
);
 
65
 
 
66
sub new {
 
67
    my $self  = shift;
 
68
    my $class = ref($self) || $self;
 
69
    my $opt   = arghash(@_);
 
70
 
 
71
    $opt->{die_on_bad_params} = 0;    # force to avoid blow-ups
 
72
 
 
73
    my %opt2 = %$opt;
 
74
    delete $opt2{virtual};
 
75
    delete $opt2{file};
 
76
    delete $opt2{string};
 
77
    $opt->{engine} = CGI::SSI->new(%opt2);
 
78
 
 
79
    return bless $opt, $class;     # rebless
 
80
}
 
81
 
 
82
sub engine {
 
83
    return shift()->{engine};
 
84
}
 
85
 
 
86
sub render {
 
87
    my $self = shift;
 
88
    my $tvar = shift || puke "Missing template expansion hashref (\$form->prepare failed?)";
 
89
 
 
90
    while(my($to, $from) = each %FORM_VARS) {
 
91
        debug 1, "renaming attr $from to: <!--#echo var=\"$to\">";
 
92
        $tvar->{$to} = "$tvar->{$from}";
 
93
    }
 
94
 
 
95
    #
 
96
    # For CGI::SSI, each data struct is manually assigned
 
97
    # to a separate <!--#echo var=... -->"
 
98
    #
 
99
    my @fieldlist;
 
100
    for my $field (@{$tvar->{fields}}) {
 
101
 
 
102
        # Field name is usually a good idea
 
103
        my $name = $field->{name};
 
104
        debug 1, "expanding field: $name";
 
105
 
 
106
        # Get all values
 
107
        my @value   = @{$tvar->{field}{$name}{values}  || []};
 
108
        my @options = @{$tvar->{field}{$name}{options} || []};
 
109
 
 
110
        #
 
111
        # Auto-expand all of our field tags, such as field, label, value
 
112
        # comment, error, etc, etc
 
113
        #
 
114
        my %all_loop;
 
115
        while(my($key, $str) = each %FIELD_VARS) {
 
116
            my $var = sprintf $str, $name;
 
117
            $all_loop{$key} = $tvar->{field}{$name}{$key};
 
118
            $tvar->{$var}   = "$tvar->{field}{$name}{$key}";   # fuck Perl
 
119
            debug 2, "<!--#echo var=\"$var\"> = " . $all_loop{$str};
 
120
        }
 
121
    }
 
122
    # kill our previous fields list
 
123
    $tvar->{fields} = \@fieldlist;
 
124
 
 
125
    # loop thru each field we have and set the tmpl_param
 
126
    while(my($param, $tag) = each %$tvar) {
 
127
        $self->{engine}->set($param => $tag);
 
128
    }
 
129
 
 
130
    # template output
 
131
    SWITCH: {
 
132
        if($self->{virtual}) {
 
133
            return $self->engine->include(virtual=>$self->{virtual});
 
134
        }
 
135
        if($self->{file}) {
 
136
            return $self->engine->include(file=>$self->{file});
 
137
        }
 
138
        if($self->{string}) {
 
139
            return $self->engine->process($self->{string});
 
140
        }
 
141
    }
 
142
}
 
143
 
 
144
1;
 
145
__END__
 
146
 
 
147
=head1 DESCRIPTION
 
148
 
 
149
This engine adapts B<FormBuilder> to use C<CGI::SSI>.
 
150
 
 
151
You can specify any options which C<< CGI::SSI->new >>
 
152
accepts by using a hashref:
 
153
 
 
154
    my $form = CGI::FormBuilder->new(
 
155
                    fields => \@fields,
 
156
                    template => {
 
157
                        type => 'CGI::SSI',
 
158
                        file => 'form.shtml',
 
159
                        sizefmt => 'abbrev'
 
160
                    }
 
161
                );
 
162
 
 
163
In addition to CGI::SSI B<new> arguments, you can also
 
164
specify C<file>, C<virtual>, or C<string> argument.
 
165
 
 
166
The following methods are provided (usually only used internally):
 
167
 
 
168
=head2 engine
 
169
 
 
170
Returns a reference to the C<CGI::SSI> object
 
171
 
 
172
=head2 prepare
 
173
 
 
174
Returns a hash of all the fields ready to be rendered.
 
175
 
 
176
=head2 render
 
177
 
 
178
Uses the prepared hash and expands the template, returning a string of HTML.
 
179
 
 
180
=head1 TEMPLATES
 
181
 
 
182
In your template, each of the form fields will correspond directly to
 
183
a C<< <!--#echo --> >> of the same name prefixed with "field-" in the
 
184
template. So, if you defined a field called "email", then you would
 
185
setup a variable called C<< <!--#echo var="field-email" --> >> in your template.
 
186
 
 
187
In addition, there are a couple special fields:
 
188
 
 
189
    <!--#echo var="js-head" -->     -  JavaScript to stick in <head>
 
190
    <!--#echo var="form-title" -->  -  The <title> of the HTML form
 
191
    <!--#echo var="form-start" -->  -  Opening <form> tag and internal fields
 
192
    <!--#echo var="form-submit" --> -  The submit button(s)
 
193
    <!--#echo var="form-reset" -->  -  The reset button
 
194
    <!--#echo var="form-end" -->    -  Just the closing </form> tag
 
195
 
 
196
Let's look at an example C<form.html> template we could use:
 
197
 
 
198
    <html>
 
199
    <head>
 
200
    <title>User Information</title>
 
201
    <!--#echo var="js-head" --><!-- this holds the JavaScript code -->
 
202
    </head>
 
203
    <!--#echo var="form-start" --><!-- this holds the initial form tag -->
 
204
    <h3>User Information</h3>
 
205
    Please fill out the following information:
 
206
    <!-- each of these <!--#echo -->'s corresponds to a field -->
 
207
    <p>Your full name: <!--#echo var="field-name" -->
 
208
    <p>Your email address: <!--#echo var="field-email" -->
 
209
    <p>Choose a password: <!--#echo var="field-password" -->
 
210
    <p>Please confirm it: <!--#echo var="field-confirm_password-->
 
211
    <p>Your home zipcode: <!--#echo var="field-zipcode -->
 
212
    <p>
 
213
    <!--#echo var="form-submit" --><!-- this holds the form submit button -->
 
214
    </form><!-- can also use "tmpl_var form-end", same thing -->
 
215
 
 
216
As you see, you get a C<< <!--#echo --> >> for each for field you define.
 
217
 
 
218
However, you may want even more control. That is, maybe you want
 
219
to specify every nitty-gritty detail of your input fields, and
 
220
just want this module to take care of the statefulness of the
 
221
values. This is no problem, since this module also provides
 
222
several other C<< <tmpl_var> >> tags as well:
 
223
 
 
224
    <!--#echo var="value-[field] -->   - The value of a given field
 
225
    <!--#echo var="label-[field] -->   - The human-readable label
 
226
    <!--#echo var="comment-[field] --> - Any optional comment
 
227
    <!--#echo var="error-[field] -->   - Error text if validation fails
 
228
    <!--#echo var="required-[field] --> - See if the field is required
 
229
 
 
230
This means you could say something like this in your template:
 
231
 
 
232
    <!--#echo var="label-email" -->:
 
233
    <input type="text" name="email" value="<!--#echo var="value-email" -->">
 
234
    <font size="-1"><i><!--#echo var="error-email" --></i></font>
 
235
 
 
236
And B<FormBuilder> would take care of the value stickiness for you,
 
237
while you have control over the specifics of the C<< <input> >> tag.
 
238
A sample expansion may create HTML like the following:
 
239
 
 
240
    Email:
 
241
    <input type="text" name="email" value="nate@wiger.org">
 
242
    <font size="-1"><i>You must enter a valid value</i></font>
 
243
 
 
244
Note, though, that this will only get the I<first> value in the case
 
245
of a multi-value parameter (for example, a multi-select list).
 
246
Multiple values (loops) in C<< CGI_SSI >> are not yet implemented.
 
247
 
 
248
For more information on templates, see L<HTML::Template>.
 
249
 
 
250
=head1 SEE ALSO
 
251
 
 
252
L<CGI::FormBuilder>, L<CGI::FormBuilder::Template>, L<HTML::Template>
 
253
 
 
254
=head1 REVISION
 
255
 
 
256
$Id: HTML.pm 97 2007-02-06 17:10:39Z nwiger $
 
257
 
 
258
=head1 AUTHOR
 
259
 
 
260
Copyright (c) L<Nate Wiger|http://nateware.com>. All Rights Reserved.
 
261
 
 
262
This module is free software; you may copy this under the terms of
 
263
the GNU General Public License, or the Artistic License, copies of
 
264
which should have accompanied your Perl kit.
 
265
 
 
266
=cut