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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-28 20:29:04 UTC
  • mfrom: (2.1.7 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080628202904-81kzjon8e8silx88
Tags: 3.05.01-6
Set urgency=medium as 3.05.01-6 included a FTBFS bugfix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
 
2
###########################################################################
 
3
# Copyright (c) 2000-2006 Nate Wiger <nate@wiger.org>. All Rights Reserved.
 
4
# Please visit www.formbuilder.org for tutorials, support, and examples.
 
5
###########################################################################
 
6
 
2
7
package CGI::FormBuilder::Template::HTML;
3
8
 
4
9
=head1 NAME
16
21
 
17
22
use Carp;
18
23
use strict;
19
 
 
20
 
our $VERSION = '3.03';
 
24
use warnings;
 
25
no  warnings 'uninitialized';
21
26
 
22
27
use CGI::FormBuilder::Util;
23
28
use HTML::Template;
24
29
use base 'HTML::Template';
25
30
 
 
31
our $REVISION = do { (my $r='$Revision: 100 $') =~ s/\D+//g; $r };
 
32
our $VERSION = '3.0501';
 
33
 
 
34
#
 
35
# For legacy reasons, and due to its somewhat odd interface, 
 
36
# HTML::Template vars use a completely different naming scheme.
 
37
#
 
38
our %FORM_VARS = (
 
39
    'js-head'       =>  'jshead',
 
40
    'form-title'    =>  'title',
 
41
    'form-start'    =>  'start',
 
42
    'form-submit'   =>  'submit',
 
43
    'form-reset'    =>  'reset',
 
44
    'form-end'      =>  'end',
 
45
    'form-invalid'  =>  'invalid',
 
46
    'form-required' =>  'required',
 
47
);
 
48
 
 
49
our %FIELD_VARS = map { $_ => "$_-%s" } qw(
 
50
    field
 
51
    value
 
52
    label
 
53
    type
 
54
    comment
 
55
    required
 
56
    error
 
57
    invalid 
 
58
    missing
 
59
    nameopts
 
60
    cleanopts
 
61
);
 
62
 
26
63
sub new {
27
64
    my $self  = shift;
28
65
    my $class = ref($self) || $self;
29
 
    my %opt   = @_;
30
 
 
31
 
    $opt{die_on_bad_params} = 0;    # force to avoid blow-ups
32
 
    $opt{engine} = HTML::Template->new(%opt);
33
 
 
34
 
    return bless \%opt, $class;     # rebless
 
66
    my $opt   = arghash(@_);
 
67
 
 
68
    $opt->{die_on_bad_params} = 0;    # force to avoid blow-ups
 
69
    $opt->{engine} = HTML::Template->new(%$opt);
 
70
 
 
71
    return bless $opt, $class;     # rebless
35
72
}
36
73
 
37
74
sub engine {
40
77
 
41
78
sub render {
42
79
    my $self = shift;
43
 
    my $form = shift;
44
 
 
45
 
    # a couple special fields
46
 
    my %tmplvar = $form->tmpl_param;
47
 
 
48
 
    # must generate JS first since it affects the others
49
 
    $tmplvar{'js-head'}     = $form->script;
50
 
    $tmplvar{'form-title'}  = $form->title;
51
 
    $tmplvar{'form-start'}  = $form->start . $form->statetags . $form->keepextras;
52
 
    $tmplvar{'form-submit'} = $form->submit;
53
 
    $tmplvar{'form-reset'}  = $form->reset;
54
 
    $tmplvar{'form-end'}    = $form->end;
55
 
 
56
 
    # for HTML::Template, each data struct is manually assigned
 
80
    my $tvar = shift || puke "Missing template expansion hashref (\$form->prepare failed?)";
 
81
 
 
82
    while(my($to, $from) = each %FORM_VARS) {
 
83
        debug 1, "renaming attr $from to: <tmpl_var $to>";
 
84
        $tvar->{$to} = "$tvar->{$from}";
 
85
    }
 
86
 
 
87
    #
 
88
    # For HTML::Template, each data struct is manually assigned
57
89
    # to a separate <tmpl_var> and <tmpl_loop> tag
58
 
    for my $field ($form->field) {
59
 
 
60
 
        # Extract value since used often
61
 
        my @value = $field->tag_value;
62
 
 
63
 
        # assign the field tag
64
 
        $tmplvar{"field-$field"} = $field->tag;
65
 
        debug 2, "<tmpl_var field-$field> = " . $tmplvar{"field-$field"};
66
 
 
67
 
        # and the value tag - can only hold first value!
68
 
        $tmplvar{"value-$field"} = $value[0];
69
 
        debug 2, "<tmpl_var value-$field> = " . $tmplvar{"value-$field"};
70
 
 
71
 
        # and the label tag for the field
72
 
        $tmplvar{"label-$field"} = $field->label;
73
 
        debug 2, "<tmpl_var label-$field> = " . $tmplvar{"value-$field"};
74
 
 
75
 
        # and the comment tag
76
 
        $tmplvar{"comment-$field"} = $field->comment;
77
 
 
78
 
        # and any error
79
 
        $tmplvar{"error-$field"} = $field->error;
80
 
 
81
 
        # create a <tmpl_loop> for multi-values/multi-opts
 
90
    #
 
91
    my @fieldlist;
 
92
    for my $field (@{$tvar->{fields}}) {
 
93
 
 
94
        # Field name is usually a good idea
 
95
        my $name = $field->{name};
 
96
        debug 1, "expanding field: $name";
 
97
 
 
98
        # Get all values
 
99
        my @value   = @{$tvar->{field}{$name}{values}  || []};
 
100
        my @options = @{$tvar->{field}{$name}{options} || []};
 
101
 
 
102
        #
 
103
        # Auto-expand all of our field tags, such as field, label, value
 
104
        # comment, error, etc, etc
 
105
        #
 
106
        my %all_loop;
 
107
        while(my($key, $str) = each %FIELD_VARS) {
 
108
            my $var = sprintf $str, $name;
 
109
            $all_loop{$key} = $tvar->{field}{$name}{$key};
 
110
            $tvar->{$var}   = "$tvar->{field}{$name}{$key}";   # fuck Perl
 
111
            debug 2, "<tmpl_var $var> = " . $all_loop{$str};
 
112
        }
 
113
 
 
114
        #
 
115
        # Create a <tmpl_loop> for multi-values/multi-opts
82
116
        # we can't include the field, really, since this would involve
83
117
        # too much effort knowing what type
 
118
        #
84
119
        my @tmpl_loop = ();
85
 
        for my $opt ($field->options) {
 
120
        for my $opt (@options) {
86
121
            # Since our data structure is a series of ['',''] things,
87
122
            # we get the name from that. If not, then it's a list
88
123
            # of regular old data that we _toname if nameopts => 1 
 
124
            debug 2, "looking at field $name option $opt";
89
125
            my($o,$n) = optval $opt;
90
 
            $n ||= $field->nameopts ? toname($o) : $o;
 
126
            $n ||= $tvar->{"nameopts-$name"} ? toname($o) : $o;
91
127
            my($slct, $chk) = ismember($o, @value) ? ('selected', 'checked') : ('','');
92
 
            debug 2, "<tmpl_loop loop-$field> = adding { label => $n, value => $o }";
 
128
            debug 2, "<tmpl_loop loop-$name> = adding { label => $n, value => $o }";
93
129
            push @tmpl_loop, {
94
130
                label => $n,
95
131
                value => $o,
98
134
            };
99
135
        }
100
136
 
101
 
        # now assign our loop-field
102
 
        $tmplvar{"loop-$field"} = \@tmpl_loop;
 
137
        # Now assign our loop-field
 
138
        $tvar->{"loop-$name"} = \@tmpl_loop;
103
139
 
104
 
        # finally, push onto a top-level loop named "fields"
105
 
        push @{$tmplvar{fields}}, {
106
 
            field   => $field->tag,
107
 
            value   => $value[0],
108
 
            values  => \@value,
109
 
            options => [ $field->options ],
110
 
            label   => $field->label,
111
 
            comment => $field->comment,
112
 
            error   => $field->error,
113
 
            loop    => \@tmpl_loop
114
 
        }
 
140
        # Finally, push onto a top-level loop named "fields"
 
141
        push @fieldlist, {
 
142
            field   => $all_loop{field},
 
143
            value   => $all_loop{value},
 
144
            values  => [ @value ],
 
145
            options => [ @options ],
 
146
            label   => $all_loop{label},
 
147
            comment => $all_loop{comment},
 
148
            error   => $all_loop{error},
 
149
            required=> $all_loop{required},
 
150
            missing => $all_loop{missing},
 
151
            fieldset=> $all_loop{fieldset},
 
152
            loop    => [ @tmpl_loop ],
 
153
        };
115
154
    }
 
155
    # kill our previous fields list
 
156
    $tvar->{fields} = \@fieldlist;
116
157
 
117
158
    # loop thru each field we have and set the tmpl_param
118
 
    while(my($param, $tag) = each %tmplvar) {
 
159
    while(my($param, $tag) = each %$tvar) {
119
160
        $self->{engine}->param($param => $tag);
120
161
    }
121
162
 
122
 
    # prepend header to template rendering
123
 
    return $form->header . $self->{engine}->output;
 
163
    # template output
 
164
    return $self->{engine}->output;
124
165
}
125
166
 
126
167
1;
150
191
                    }
151
192
                );
152
193
 
 
194
The following methods are provided (usually only used internally):
 
195
 
 
196
=head2 engine
 
197
 
 
198
Returns a reference to the C<HTML::Template> object
 
199
 
 
200
=head2 prepare
 
201
 
 
202
Returns a hash of all the fields ready to be rendered.
 
203
 
 
204
=head2 render
 
205
 
 
206
Uses the prepared hash and expands the template, returning a string of HTML.
 
207
 
 
208
=head1 TEMPLATES
 
209
 
153
210
In your template, each of the form fields will correspond directly to
154
211
a C<< <tmpl_var> >> of the same name prefixed with "field-" in the
155
212
template. So, if you defined a field called "email", then you would
196
253
    <tmpl_var label-[field]>   - The human-readable label
197
254
    <tmpl_var comment-[field]> - Any optional comment
198
255
    <tmpl_var error-[field]>   - Error text if validation fails
 
256
    <tmpl_var required-[field]> - See if the field is required
199
257
 
200
258
This means you could say something like this in your template:
201
259
 
342
400
    <table>
343
401
    <tmpl_loop fields>
344
402
    <tr>
345
 
    <td class="small"><tmpl_var label></td>
 
403
    <td class="small"><tmpl_if required><b><tmpl_var label></b><tmpl_else><tmpl_var label></tmpl_if></td>
346
404
    <td><tmpl_var field></td>
347
405
    </tr>
348
406
    </tmpl_loop>
358
416
 
359
417
=head1 REVISION
360
418
 
361
 
$Id: HTML.pm,v 1.32 2006/02/24 01:42:29 nwiger Exp $
 
419
$Id: HTML.pm 100 2007-03-02 18:13:13Z nwiger $
362
420
 
363
421
=head1 AUTHOR
364
422