File: Fast.pm

package info (click to toggle)
libcgi-formbuilder-perl 3.08-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,204 kB
  • sloc: perl: 7,201; makefile: 13
file content (214 lines) | stat: -rw-r--r-- 6,373 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

###########################################################################
# Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved.
# Please visit http://formbuilder.org for tutorials, support, and examples.
###########################################################################

package CGI::FormBuilder::Template::Fast;

=head1 NAME

CGI::FormBuilder::Template::Fast - FormBuilder interface to CGI::FastTemplate

=head1 SYNOPSIS

    my $form = CGI::FormBuilder->new(
        fields   => \@whatever,
        template => {
            type => 'Fast',
            root => '/path/to/templates',
            # use external files
            define => {
                form           => 'form.txt',
                field          => 'field.txt',
                invalid_field  => 'invalid_field.txt',
            },
            # or define inline
            define_nofile => {
                form => '<html><head></head><body>$START_FORM
                         <table>$FIELDS</table>$SUBMIT $END_FORM</body></html>',
                # etc.
            },
        },
   );

=cut

use Carp;
use strict;
use warnings;
no  warnings 'uninitialized';

use CGI::FormBuilder::Util;
use CGI::FastTemplate;
use base 'CGI::FastTemplate';


our $VERSION = '3.08';

sub new {
    my $self  = shift;
    my $class = ref($self) || $self;
    my $opt   = arghash(@_);

    my $t = CGI::FastTemplate->new($opt->{root});

    # turn off strict so that undef vars show up
    # as blank in the template output
    $t->no_strict;

    # define our templates
    $t->define(%{ $opt->{define} });
    $t->define_raw($opt->{define_raw});
    $t->define_nofile($opt->{define_nofile});    

    # jam $t info FB 'engine' container
    $opt->{engine} = $t;

    return bless $opt, $class;
}

sub engine {
    return shift()->{engine};
}

sub prepare {
    my $self = shift;
    my $form = shift;

    # a couple special fields
    my %tmplvar = $form->tmpl_param;

    # Go over the fields and process each one
    for my $field ($form->field) {
        # Extract value since used often
        my @value = $field->tag_value;

        # assign this field's variables
        my $ref = {
            NAME     => $field->name,
            FIELD    => $field->tag,
            VALUE    => $value[0],       # the VALUE tag can only hold first value!
            LABEL    => $field->label,
            REQUIRED => ($field->required ? 'required' : 'optional'),
            ERROR    => $field->error,
            COMMENT  => $field->comment,
        };
        $self->{engine}->assign($ref);

        # TODO: look for special templates based on field name or type?
        if ($field->invalid) {
            $self->{engine}->parse(FIELDS => '.invalid_field');
        } else {
            $self->{engine}->parse(FIELDS => '.field');
        }

        $self->{engine}->clear_href;        
    }

    # a couple special fields    
    $self->{engine}->assign({
        TITLE      => $form->title,
        JS_HEAD    => $form->script,
        START_FORM => $form->start . $form->statetags . $form->keepextras,
        SUBMIT     => $form->submit,
        RESET      => $form->reset,
        END_FORM   => $form->end,
        %tmplvar,
    });
    $self->{engine}->parse(FORM => 'form');

    return $self;
}

sub render {
    my $self = shift;
    return ${ $self->{engine}->fetch('FORM') };
}


# End of Perl code
1;
__END__

=head1 DESCRIPTION

This engine adapts B<FormBuilder> to use C<CGI::FastTemplate>. Please
read these docs carefully, as the usage differs from other template
adapters in several important ways.

You will need to define three templates: C<form>, C<field>, and
C<invalid_field>. You can use C<define> to point to external files
(which is the recommended C<CGI::FastTemplate> style), or C<define_nofile>/
C<define_raw> to define them inline. The templates in C<define_nofile>
take precedence over C<define_raw>, and both of these take precedence
over C<define>.

    my $form = CGI::FormBuilder->new(
        # ...
        template => {
            type => 'FastTemplate',
            root => '/path/to/templates',
            define => {
                form           => 'form.txt',
                field          => 'field.txt',
                invalid_field  => 'invalid_field.txt',
            },
            # or, you can define templates directly
            define_nofile => {
                form => '<html><head></head><body>$START_FORM<table>'
                        '$FIELDS</table>$SUBMIT $END_FORM</body></html>',
                # etc.
            },
        },
        # ...
    );

If you use C<define> with external templates, you will probably
also want to define your template root directory with the C<root>
parameter.

Within each of the field templates, the following variables
are available:

    $NAME         # $field->name
    $FIELD        # $field->tag   (HTML input tag)
    $VALUE        # $field->value (first value only!)
    $LABEL        # $field->label
    $COMMENT      # $field->comment
    $ERROR        # $field->error
    $REQUIRED     # $field->required ? 'required' : 'optional'

All the fields are processed in sequence; valid fields use the 
C<field> template, and invalid fields the C<invalid_field> template.
The result from each of these is appended into the C<$FIELDS>
variable, which you should use in your C<form> template. In the 
C<form> template, you also have access to these variables:

    $TITLE        # title of the form
    $START_FORM   # opening form tag
    $SUBMIT       # the submit button
    $RESET        # the reset button
    $END_FORM     # closing form tag
    $JS_HEAD      # validation JavaScript

Note that since C<CGI::FastTemplate> doesn't use anything other than 
simple scalar variables, there are no variables corrosponding to 
the lists that other engines have (e.g. C<fields> or C<options> 
lists in C<TT2> or C<Text::Template>).

=head1 SEE ALSO

L<CGI::FormBuilder>, L<CGI::FormBuilder::Template>, L<CGI::FastTemplate>

=head1 AUTHOR

Copyright (c) 2005-2006 Peter Eichman <peichman@cpan.org>. All Rights Reserved.

Maintained as part of C<CGI::FormBuilder> by Nate Wiger <nate@wiger.org>. 

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