package HTML::FormHandler::Manual::Templates; # ABSTRACT: using templates __END__ =pod =encoding UTF-8 =head1 NAME HTML::FormHandler::Manual::Templates - using templates =head1 VERSION version 0.40068 =head1 SYNOPSIS L Documentation on templates to use with L =head1 Using templates There is a FormHandler Template Toolkit rendering role at L, with a testcase in t/render_withtt.t. Normally, however, it probably won't make much sense to use both a TT parser in FormHandler, and a separate one for the "complete" templates, so the TT renderer is mainly useful for tests, or as an example of how to do TT rendering with HFH. You should create a template to render your form and then pass the template name and a template variable containing your form object to your templating or view engine, in whatever way you normally do that. If you want to use the 'process_attrs' function, you need to set that in your template variables too. A common way of using FormHandler with templates is to use the template for layout, specifying the divs and spans and wrappers, and then use the form object to render just the input fields. In your form: has '+widget_wrapper' => ( default => 'None' ); In your Catalyst controller: $c->stash( form => $form, template => 'form.tt' ); ..or do the equivalent for your web framework/view. In a form template (form.tt in the previous controller example):
My Foo [% form.field('foo').render %]
[% form.field('bar').render %]
[% form.field('save').render %]
However, you can also render entirely with templates. There are lots of different ways to set up templates. There are sample templates installed in FormHandler's 'share' directory. These templates are now organized more-or-less similarly to the widget roles, with 'field', 'wrapper', and 'form' directories, but many other organizations are possible. There is also a template which combines the template rendering code into one file, 'share/templates/form/form_in_one.tt'. You can copy this template into your own TT directories, perhaps as form.tt, and then specify it as the template for your Catalyst actions. You can customize it by adding additional widget and widget_wrapper blocks, and then setting those in your field definitions. Note that widget names usually are camelcased, like the Moose roles that implement them in the Widget directory. You may want to use the non-camelcased widget/wrapper names in your TT templates, using the C<< $field->uwidget >> (un-camelcased widget name) and C<< $field->twidget >> (un-camelcased widget name + '.tt') convenience methods. ('MySpecialWidget' is the equivalent of 'my_special_widget') has_field 'my_field' => ( widget => 'MySpecialWidget' ); has_field 'another_field' => ( widget => 'YetAnotherWidget' ); And include them in a generic template: [% PROCESS widget/form_start.tt %] [% FOREACH f IN form.sorted_fields %] [% PROCESS widget/${f.twidget} %] [% END %] [% PROCESS widget/form_end.tt %] =head1 Field attributes If you want to use the 'process_attrs' function to pull in HTML attributes for the input elements, wrappers, and labels, you would need to pass that function into your TT setup. See L for an example: use HTML::FormHandler::Render::Util ('process_attrs'); $c->stash( process_attrs => &process_attrs ); # or add to TT vars in your view =head1 Sample templates The following is copied from the provided share/templates/form/form_in_one.tt file, as an example. Note that some fields, like form actions of 'submit' & 'reset', don't use the 'fif' value, but just the plain field value. [% PROCESS form_start -%]
[% FOREACH err IN form.form_errors -%] [% err %] [% END -%]
[% FOREACH f IN form.sorted_fields -%] [% WRAPPER "wrapper_${f.uwrapper}" -%][% PROCESS "${f.uwidget}" -%][% END -%] [% END -%] [% PROCESS form_end -%] [% BLOCK form_start -%] [% END -%] [% BLOCK form_end -%] [% END -%] [% BLOCK button -%] [% END -%] [% BLOCK checkbox -%] [%~ ~%] [% END -%] [% BLOCK checkbox_group -%] [% FOR option IN f.options -%] [% END -%] [% END -%] [% BLOCK compound -%] [% FOREACH sf IN f.sorted_fields -%] [% outerf = f; f = sf; -%] [% WRAPPER "wrapper_${f.uwrapper}" %][% PROCESS "${f.uwidget}" -%][% END -%] [% f = outerf -%] [% END -%] [% END -%] [% BLOCK hidden -%] [% END -%] [% BLOCK password -%] [% END -%] [% BLOCK radio_group -%] [% FOR option IN f.options -%] [% END -%] [% END -%] [% BLOCK repeatable -%] [% FOREACH rf IN f.sorted_fields -%] [% outerrf = f; f = rf; -%] [% WRAPPER "wrapper_${f.uwrapper}" %][% PROCESS "${f.uwidget}" -%][% END -%] [% f = outerrf -%] [% END -%] [% END -%] [% BLOCK reset -%] [% END -%] [% BLOCK select -%] [% END -%] [% BLOCK submit -%] [% END -%] [% BLOCK text -%] [% END -%] [% BLOCK textarea -%] [% END -%] [% BLOCK upload -%] [% END -%] [% BLOCK wrapper_simple -%] [% IF f.do_label %][% PROCESS label %][% END -%] [% content -%] [% END -%] [% BLOCK label -%] [% END -%] [% BLOCK wrapper_wrap_label -%] [%~ content ~%][%~ f.label %] [% END -%] [% BLOCK wrapper_none -%] [% content %] [% END -%] [% BLOCK wrapper_fieldset -%] [% f.label %] [% content -%] [% END -%] =head1 AUTHOR FormHandler Contributors - see HTML::FormHandler =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2017 by Gerda Shank. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut