The Perl Advent Calendar needs more articles for 2022. Submit your idea today!

NAME

CGI::FormBuilder::Template::HTC

SYNOPSIS

    my $form = CGI::FormBuilder->new(
        fields   => \@fields,
        template => {
            type     => 'HTC',
            filename => 'form.tmpl',
            variable => 'form',
            # other HTC options
        },
        data => {
            # other parameters for the template object
        },
    );

DESCRIPTION

This engine adapts FormBuilder to use HTML::Template::Compiled. It works similar to CGI::FormBuilder::Template::TT2, not like CGI::FormBuilder::Template::HTML, because HTC can use the dot-syntax for accessing hash-keys.

METHODS

engine

Returns the HTML::Template::Compiled object.

render

Uses the prepared hash and expands the template, returning a string of HTML.

new
engine_class

Returns the class of the template engine (HTML::Template::Compiled)

TEMPLATES

The template might look something like this (this is HTC syntax):

    <html>
    <head>
      <title>[%= form.title %]</title>
      [%= form.jshead %]
    </head>
    <body>
      [%= form.start %]
      <table>
        [%loop form.fields %]
        <tr valign="top">
          <td>
            [%if required %]
                <b>[%= label %]</b>
            [%else %]
                [%= label %]
            [%/if %]
          </td>
          <td>
            [%if invalid %]
            Missing or invalid entry, please try again.
            <br/>
            [%/if %]

        [%= field %]
      </td>
    </tr>
        [%/loop %]
        <tr>
          <td colspan="2" align="center">
            [%= form.submit %] [%= form.reset %]
          </td>
        </tr>
      </table>
      [%= form.end %]
    </body>
    </html>

By default, all the form and field information are accessible through simple variables.

    [%= jshead %]  -  JavaScript to stick in <head>
    [%= title  %]  -  The <title> of the HTML form
    [%= start  %]  -  Opening <form> tag and internal fields
    [%= submit %]  -  The submit button(s)
    [%= reset  %]  -  The reset button
    [%= end    %]  -  Closing </form> tag
    [%= fields %]  -  List of fields
    [%= field  %]  -  Hash of fields (for lookup by name)

You can specify the variable option to have all these variables accessible under a certain namespace. For example:

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

With variable set to form the variables are accessible as:

    [%= form.jshead %]
    [%= form.start  %]
    etc.

You can access individual fields via the field variable.

    For a field named...  The field data is in...
    --------------------  -----------------------
    job                   [%= form.field.job   %]
    size                  [%= form.field.size  %]
    email                 [%= form.field.email %]

Each field contains various elements. For example:

    [%with form.field.email %]

    [%= label    %]  # text label
    [%= field    %]  # field input tag
    [%= value    %]  # first value
    [%= values   %]  # list of all values
    [%= option   %]  # first value
    [%= options  %]  # list of all values
    [%= required %]  # required flag
    [%= invalid  %]  # invalid flag

    [%/with %]

The fields variable contains a list of all the fields in the form. To iterate through all the fields in order, you could do something like this:

    [%loop form.fields %]
    <tr>
     <td>[%= label %]</td> <td>[%= field %]</td>
    </tr>
    [%/loop %]

If you want to customise any of the HTC options, you can add options to the template option. You can also set the data item to define any additional variables you want accesible when the template is processed.

    my $form = CGI::FormBuilder->new(
        fields   => \@fields,
        template => {
            type     => 'HTC',
            filename => 'form.tmpl',
            variable => 'form',
            # other HTC options
            cache_dir => '/path/to/cachedir',
        },
        data => {
              version => 1.23,
              author  => 'Fred Smith',
        },
    );

For further details on using the Template Toolkit, see

SEE ALSO

CGI::FormBuilder
CGI::FormBuilder::Template::TT2
CGI::FormBuilder::Template::HTML
HTML::Template::Compiled

AUTHOR

Tina Mueller

CREDITS

Nate Wiger, who is the author of CGI::FormBuilder. I copied more or less the whole documentation and some of the tests.

pfuschi from perl-community.de for the idae

COPYRIGHT AND LICENSE

Copyright (C) 2006 by Tina Mueller

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.3 or, at your option, any later version of Perl 5 you may have available.