1HTML::FormHandler::ManuUasle:r:TCeomnptlraitbeust(e3d)PHeTrMlL:D:oFcourmmeHnatnadtlieorn::Manual::Templates(3)
2
3
4

NAME

6       HTML::FormHandler::Manual::Templates - using templates
7

VERSION

9       version 0.40068
10

SYNOPSIS

12       Manual Index
13
14       Documentation on templates to use with HTML::FormHandler
15

Using templates

17       There is a FormHandler Template Toolkit rendering role at
18       HTML::FormHandler::Render::WithTT, with a testcase in
19       t/render_withtt.t.  Normally, however, it probably won't make much
20       sense to use both a TT parser in FormHandler, and a separate one for
21       the "complete" templates, so the TT renderer is mainly useful for
22       tests, or as an example of how to do TT rendering with HFH. You should
23       create a template to render your form and then pass the template name
24       and a template variable containing your form object to your templating
25       or view engine, in whatever way you normally do that. If you want to
26       use the 'process_attrs' function, you need to set that in your template
27       variables too.
28
29       A common way of using FormHandler with templates is to use the template
30       for layout, specifying the divs and spans and wrappers, and then use
31       the form object to render just the input fields.
32
33       In your form:
34
35           has '+widget_wrapper' => ( default => 'None' );
36
37       In your Catalyst controller:
38
39           $c->stash( form => $form, template => 'form.tt' );
40
41       ..or do the equivalent for your web framework/view.
42
43       In a form template (form.tt in the previous controller example):
44
45           <form id="myform" action="/myaction" method="post">
46               <div class="span9">
47                   <span class="label">My Foo</span>
48                   [% form.field('foo').render %]
49               </div>
50               <div class="span7">[% form.field('bar').render %]</div>
51               [% form.field('save').render %]
52           </form>
53
54       However, you can also render entirely with templates.  There are lots
55       of different ways to set up templates. There are sample templates
56       installed in FormHandler's 'share' directory. These templates are now
57       organized more-or-less similarly to the widget roles, with 'field',
58       'wrapper', and 'form' directories, but many other organizations are
59       possible.
60
61       There is also a template which combines the template rendering code
62       into one file, 'share/templates/form/form_in_one.tt'. You can copy this
63       template into your own TT directories, perhaps as form.tt, and then
64       specify it as the template for your Catalyst actions. You can customize
65       it by adding additional widget and widget_wrapper blocks, and then
66       setting those in your field definitions.
67
68       Note that widget names usually are camelcased, like the Moose roles
69       that implement them in the Widget directory. You may want to use the
70       non-camelcased widget/wrapper names in your TT templates, using the
71       "$field->uwidget" (un-camelcased widget name) and "$field->twidget"
72       (un-camelcased widget name + '.tt') convenience methods.
73       ('MySpecialWidget' is the equivalent of 'my_special_widget')
74
75          has_field 'my_field' => ( widget => 'MySpecialWidget' );
76          has_field 'another_field' => ( widget => 'YetAnotherWidget' );
77
78       And include them in a generic template:
79
80          [% PROCESS widget/form_start.tt %]
81
82          [% FOREACH f IN form.sorted_fields %]
83             [% PROCESS widget/${f.twidget} %]
84          [% END %]
85
86          [% PROCESS widget/form_end.tt %]
87

Field attributes

89       If you want to use the 'process_attrs' function to pull in HTML
90       attributes for the input elements, wrappers, and labels, you would need
91       to pass that function into your TT setup. See
92       HTML::FormHandler::Render::WithTT for an example:
93
94           use HTML::FormHandler::Render::Util ('process_attrs');
95           $c->stash( process_attrs => &process_attrs ); # or add to TT vars in your view
96
97           <label [% process_attrs(f.label_attributes) %]for="[% f.html_name %]">
98           [% f.label %]: </label>
99           <input type="[% f.input_type %]" name="[% f.html_name %]" id="[% f.id %]"
100           [% process_attrs(f.attributes) %] value="[% f.fif %]">
101

Sample templates

103       The following is copied from the provided
104       share/templates/form/form_in_one.tt file, as an example. Note that some
105       fields, like form actions of 'submit' & 'reset', don't use the 'fif'
106       value, but just the plain field value.
107
108           [% PROCESS form_start -%]
109           <div class="form_messages">
110           [% FOREACH err IN form.form_errors -%]
111             <span class="error_message">[% err %]</span>
112           [% END -%]
113           </div>
114           [% FOREACH f IN form.sorted_fields -%]
115             [% WRAPPER "wrapper_${f.uwrapper}" -%][% PROCESS "${f.uwidget}" -%][% END -%]
116           [% END -%]
117           [% PROCESS form_end -%]
118
119           [% BLOCK form_start -%]
120           <form[% process_attrs(form.attributes) %]>
121           [% END -%]
122
123           [% BLOCK form_end -%]
124           </form>
125           [% END -%]
126
127           [% BLOCK button -%]
128           <input type="button" name="[% f.html_name %]" id="[% f.id %]"
129               [% process_attrs(f.attributes) %] value="[% f.value %]" />
130           [% END -%]
131
132           [% BLOCK checkbox -%]
133           <input type="checkbox" name="[% f.html_name %]" id="[% f.id %]"
134              [% process_attrs(f.attributes) %] value="[% f.checkbox_value -%]"
135              [% IF f.fif == f.checkbox_value -%] checked="checked"[% END ~%] />[%~ ~%]
136           [% END -%]
137
138           [% BLOCK checkbox_group -%]
139           [% FOR option IN f.options -%]
140             <label class="checkbox" for="[% f.name %].[% loop.index %]">
141             <input type="checkbox" value="[% option.value %]" name="[% f.name %]"
142                id="[% f.name %].[% loop.index %]"
143                [% FOREACH selval IN f.fif -%]
144                  [% IF selval == option.value %] checked="checked"[% END -%]
145                [% END -%]>
146             [% option.label | html %]
147             </label>
148           [% END -%]
149           [% END -%]
150
151           [% BLOCK compound -%]
152           [% FOREACH sf IN f.sorted_fields -%]
153             [% outerf = f; f = sf; -%]
154             [% WRAPPER "wrapper_${f.uwrapper}" %][% PROCESS "${f.uwidget}" -%][% END -%]
155             [% f = outerf -%]
156           [% END -%]
157           [% END -%]
158
159           [% BLOCK hidden -%]
160           <input type="hidden" name="[% f.html_name %]" id="[% f.id %]"
161               [% process_attrs(f.attributes) %] value="[% f.fif %]" />
162           [% END -%]
163
164           [% BLOCK password -%]
165           <input type="password" name="[% f.html_name %]" id="[% f.id %]"
166               [% process_attrs(f.attributes) %] value="[% f.fif %]" />
167           [% END -%]
168
169           [% BLOCK radio_group -%]
170           [% FOR option IN f.options -%]
171             <label for="[% f.id %].[% loop.index %]">
172             <input type="radio" value="[% option.value %]" name="[% f.name %]"
173               id="[% f.id %].[% loop.index %]"
174               [% IF option.value == f.fif %] checked="checked"[% END %] />
175             [% option.label %]
176             </label>
177           [% END -%]
178           [% END -%]
179
180           [% BLOCK repeatable -%]
181           [% FOREACH rf IN f.sorted_fields -%]
182             [% outerrf = f; f = rf; -%]
183             [% WRAPPER "wrapper_${f.uwrapper}" %][% PROCESS "${f.uwidget}" -%][% END -%]
184             [% f = outerrf -%]
185           [% END -%]
186           [% END -%]
187
188           [% BLOCK reset -%]
189           <input type="reset" name="[% f.html_name %]" id="[% f.id %]"
190               [% process_attrs(f.attributes) %] value="[% f.value %]" />
191           [% END -%]
192
193           [% BLOCK select -%]
194           <select name="[% f.html_name %]" id="[% f.id %]"[% process_attrs(f.attributes) %]
195             [% IF f.multiple %] multiple="multiple" size="[% f.size %]" [% END -%]>
196             [% FOR option IN f.options -%]
197             <option id="[% f.id %].[% loop.index %]" value="[% option.value -%]"
198                 [% FOREACH selval IN f.fif -%]
199                   [% IF selval == option.value %] selected="selected"[% END -%]
200                 [% END -%]>
201                 [% option.label | html %]
202             </option>
203             [% END -%]
204           </select>
205           [% END -%]
206
207           [% BLOCK submit -%]
208           <input type="submit" name="[% f.html_name %]" id="[% f.id %]"
209               [% process_attrs(f.attributes) %] value="[% f.value %]" />
210           [% END -%]
211
212           [% BLOCK text -%]
213           <input type="[% f.input_type %]" name="[% f.html_name %]" id="[% f.id %]"
214              [% process_attrs(f.attributes) %] value="[% f.fif %]" />
215           [% END -%]
216
217           [% BLOCK textarea -%]
218           <textarea name="[% f.html_name %]" id="[% f.id %]" rows="[% f.rows %]"
219              cols="[% f.cols %]" [% process_attrs(f.attributes) %]>[% f.fif %]</textarea>
220           [% END -%]
221
222           [% BLOCK upload -%]
223           <input type="file" name="[% f.html_name %]" id="[% f.html_name %]"
224               [% process_attrs(f.attributes) %] />
225           [% END -%]
226
227           [% BLOCK wrapper_simple -%]
228           <div[% process_attrs(f.wrapper_attributes) -%]>
229             [% IF f.do_label %][% PROCESS label %][% END -%]
230             [% content -%]
231           </div>
232           [% END -%]
233
234           [% BLOCK label -%]
235           <label [% process_attrs(f.label_attributes) %]for="[% f.html_name %]">[% f.label %]</label>
236           [% END -%]
237
238           [% BLOCK wrapper_wrap_label -%]
239           <div[% process_attrs(f.wrapper_attributes) %]>
240               <label[% process_attrs(f.label_attributes) %] for="[% f.html_name %]">
241                  [%~ content ~%][%~ f.label %]
242               </label>
243           </div>
244           [% END -%]
245
246           [% BLOCK wrapper_none -%]
247           [% content %]
248           [% END -%]
249
250           [% BLOCK wrapper_fieldset -%]
251           <fieldset[% process_attrs(f.wrapper_attributes)%]><legend>[% f.label %]</legend>
252           [% content -%]
253           </fieldset>
254           [% END -%]
255

AUTHOR

257       FormHandler Contributors - see HTML::FormHandler
258
260       This software is copyright (c) 2017 by Gerda Shank.
261
262       This is free software; you can redistribute it and/or modify it under
263       the same terms as the Perl 5 programming language system itself.
264
265
266
267perl v5.38.0                      2023-0H7T-M2L0::FormHandler::Manual::Templates(3)
Impressum