File: Template.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 (209 lines) | stat: -rw-r--r-- 6,411 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

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

package CGI::FormBuilder::Template;

=head1 NAME

CGI::FormBuilder::Template - Template adapters for FormBuilder

=head1 SYNOPSIS

    # Define a template engine

    package CGI::FormBuilder::Template::Whatever;
    use base 'Whatever::Template::Module';

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

        # override some options
        $opt{some_setting} = 0;
        $opt{another_var}  = 'Some Value';

        # instantiate the template engine
        $opt{engine} = Whatever::Template::Module->new(%opt);

        return bless \%opt, $class;
    }

    sub render {
        my $self = shift;
        my $form = shift;   # only arg is form object

        # grab any manually-set template params
        my %tmplvar = $form->tmpl_param;

        # example template manipulation
        my $html = $self->{engine}->do_template(%tmplvar);

        return $html;       # scalar HTML is returned
    }

=cut

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

our $VERSION = '3.08';
warn __PACKAGE__, " is not a real module, please read the docs\n"; 
1;
__END__

=head1 DESCRIPTION

This documentation describes the usage of B<FormBuilder> templates,
as well as how to write your own template adapter.

The template engines serve as adapters between CPAN template modules
and B<FormBuilder>. A template engine is invoked by using the C<template>
option to the top-level C<new()> method:

    my $form = CGI::FormBuilder->new(
                    template => 'filename.tmpl'
               );

This example points to a filename that contains an C<HTML::Template>
compatible template to use to layout the HTML. You can also specify
the C<template> option as a reference to a hash, allowing you to
further customize the template processing options, or use other
template engines.

For example, you could turn on caching in C<HTML::Template> with
something like the following:

    my $form = CGI::FormBuilder->new(
                    fields => \@fields,
                    template => {
                        filename => 'form.tmpl',
                        shared_cache => 1
                    }
               );

As mentioned, specifying a hashref allows you to use an alternate template
processing system like the C<Template Toolkit>.  A minimal configuration
would look like this:

    my $form = CGI::FormBuilder->new(
                    fields => \@fields,
                    template => {
                        type => 'TT2',      # use Template Toolkit
                        template => 'form.tmpl',
                    },
               );

The C<type> option specifies the name of the engine. Currently accepted
types are:

    Builtin -  Included, default rendering if no template specified
    Div     -  Render form using <div> (no tables)
    HTML    -  HTML::Template
    Text    -  Text::Template
    TT2     -  Template Toolkit
    Fast    -  CGI::FastTemplate
    CGI_SSI -  CGI::SSI

In addition to one of these types, you can also specify a complete package name,
in which case that module will be autoloaded and its C<new()> and C<render()>
routines used. For example:

    my $form = CGI::FormBuilder->new(
                    fields => \@fields,
                    template => {
                        type => 'My::Template::Module',
                        template => 'form.tmpl',
                    },
               );

All other options besides C<type> are passed to the constructor for that
templating system verbatim, so you'll need to consult those docs to see what
all the different options do. Skip down to L</"SEE ALSO">.

=head1 SUBCLASSING TEMPLATE ADAPTERS

In addition to the above included template engines, it is also possible to write
your own rendering module. If you come up with something cool, please let the
mailing list know!

To do so, you need to write a module which has a sub called C<render()>. This
sub will be called by B<FormBuilder> when C<< $form->render >> is called. This
sub can do basically whatever it wants, the only thing it has to do is return
a scalar string which is the HTML to print out. 

This is actually not hard. Here's a simple adapter which would manipulate
an C<HTML::Template> style template:

    # This file is My/HTML/Template.pm
    package My::HTML::Template;

    use CGI::FormBuilder::Template::HTML;
    use base 'CGI::FormBuilder::Template::HTML';

    sub render {
        my $self = shift;    # class object
        my $form = shift;    # $form as only argument

        # the template object (engine) lives here
        my $tmpl = $self->engine;

        # setup vars for our fields (objects)
        for ($form->field) {
            $tmpl->param($_ => $_->value);
        }

        # render output
        my $html = $tmpl->output;

        # return scalar;
        return $html;
    }
    1;  # close module

Then in B<FormBuilder>:

    use CGI::FormBuilder;
    use My::HTML::Template;   # your module

    my $tmpl = My::HTML::Template->new;

    my $form = CGI::FormBuilder->new(
                    fields   => [qw(name email)],
                    header   => 1,
                    template => $tmpl   # pass template object
               );

    # set our company from an extra CGI param
    my $co = $form->cgi_param('company');
    $tmpl->engine->param(company => $co);

    # and render like normal
    print $form->render;

That's it! For more details, the best thing to do is look through
the guts of one of the existing template engines and go from there.

=head1 SEE ALSO

L<CGI::FormBuilder>, L<CGI::FormBuilder::Template::HTML>,
L<CGI::FormBuilder::Template::Text>, L<CGI::FormBuilder::Template::TT2>,
L<CGI::FormBuilder::Template::Fast>, L<CGI::FormBuilder::Template::CGI_SSI>

=head1 REVISION

$Id: Template.pm 97 2007-02-06 17:10:39Z nwiger $

=head1 AUTHOR

Copyright (c) L<Nate Wiger|http://nateware.com>. 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