1CGI::FormBuilder::TemplUasteer::CToTn2t(r3i)buted Perl DCoGcIu:m:eFnotramtBiuoinlder::Template::TT2(3)
2
3
4
6 CGI::FormBuilder::Template::TT2 - FormBuilder interface to Template
7 Toolkit
8
10 my $form = CGI::FormBuilder->new(
11 fields => \@fields,
12 template => {
13 type => 'TT2',
14 template => 'form.tmpl',
15 variable => 'form',
16 }
17 );
18
20 This engine adapts FormBuilder to use "Template Toolkit". To do so,
21 specify the "template" option as a hashref which includes the "type"
22 option set to "TT2" and the "template" option set to the name of the
23 template you want processed. You can also add "variable" as an option
24 (among others) to denote the variable name that you want the form data
25 to be referenced by:
26
27 my $form = CGI::FormBuilder->new(
28 fields => \@fields,
29 template => {
30 type => 'TT2',
31 template => 'userinfo.tmpl',
32 variable => 'form',
33 }
34 );
35
36 The following methods are provided (usually only used internally):
37
38 engine
39 Returns a reference to the "Template" object
40
41 prepare
42 Returns a hash of all the fields ready to be rendered.
43
44 render
45 Uses the prepared hash and expands the template, returning a string of
46 HTML.
47
49 The template might look something like this:
50
51 <html>
52 <head>
53 <title>[% form.title %]</title>
54 [% form.jshead %]
55 </head>
56 <body>
57 [% form.start %]
58 <table>
59 [% FOREACH field = form.fields %]
60 <tr valign="top">
61 <td>
62 [% field.required
63 ? "<b>$field.label</b>"
64 : field.label
65 %]
66 </td>
67 <td>
68 [% IF field.invalid %]
69 Missing or invalid entry, please try again.
70 <br/>
71 [% END %]
72
73 [% field.field %]
74 </td>
75 </tr>
76 [% END %]
77 <tr>
78 <td colspan="2" align="center">
79 [% form.submit %] [% form.reset %]
80 </td>
81 </tr>
82 </table>
83 [% form.end %]
84 </body>
85 </html>
86
87 By default, the Template Toolkit makes all the form and field
88 information accessible through simple variables.
89
90 [% jshead %] - JavaScript to stick in <head>
91 [% title %] - The <title> of the HTML form
92 [% start %] - Opening <form> tag and internal fields
93 [% submit %] - The submit button(s)
94 [% reset %] - The reset button
95 [% end %] - Closing </form> tag
96 [% fields %] - List of fields
97 [% field %] - Hash of fields (for lookup by name)
98
99 You can specify the "variable" option to have all these variables
100 accessible under a certain namespace. For example:
101
102 my $form = CGI::FormBuilder->new(
103 fields => \@fields,
104 template => {
105 type => 'TT2',
106 template => 'form.tmpl',
107 variable => 'form'
108 },
109 );
110
111 With "variable" set to "form" the variables are accessible as:
112
113 [% form.jshead %]
114 [% form.start %]
115 etc.
116
117 You can access individual fields via the "field" variable.
118
119 For a field named... The field data is in...
120 -------------------- -----------------------
121 job [% form.field.job %]
122 size [% form.field.size %]
123 email [% form.field.email %]
124
125 Each field contains various elements. For example:
126
127 [% myfield = form.field.email %]
128
129 [% myfield.label %] # text label
130 [% myfield.field %] # field input tag
131 [% myfield.value %] # first value
132 [% myfield.values %] # list of all values
133 [% myfield.option %] # first value
134 [% myfield.options %] # list of all values
135 [% myfield.required %] # required flag
136 [% myfield.invalid %] # invalid flag
137
138 The "fields" variable contains a list of all the fields in the form.
139 To iterate through all the fields in order, you could do something like
140 this:
141
142 [% FOREACH field = form.fields %]
143 <tr>
144 <td>[% field.label %]</td> <td>[% field.field %]</td>
145 </tr>
146 [% END %]
147
148 If you want to customise any of the Template Toolkit options, you can
149 set the "engine" option to contain a reference to an existing
150 "Template" object or hash reference of options which are passed to the
151 "Template" constructor. You can also set the "data" item to define any
152 additional variables you want accesible when the template is processed.
153
154 my $form = CGI::FormBuilder->new(
155 fields => \@fields,
156 template => {
157 type => 'TT2',
158 template => 'form.tmpl',
159 variable => 'form',
160 engine => {
161 INCLUDE_PATH => '/usr/local/tt2/templates',
162 },
163 data => {
164 version => 1.23,
165 author => 'Fred Smith',
166 },
167 },
168 );
169
170 For further details on using the Template Toolkit, see "Template" or
171 <http://www.template-toolkit.org>
172
174 CGI::FormBuilder, CGI::FormBuilder::Template, Template
175
177 $Id: TT2.pm 100 2007-03-02 18:13:13Z nwiger $
178
180 Copyright (c) Nate Wiger <http://nateware.com>. All Rights Reserved.
181
182 Template Tookit support is largely due to a huge patch from Andy
183 Wardley.
184
185 This module is free software; you may copy this under the terms of the
186 GNU General Public License, or the Artistic License, copies of which
187 should have accompanied your Perl kit.
188
189
190
191perl v5.38.0 2023-07-20CGI::FormBuilder::Template::TT2(3)