# phpman > perldoc > CGI::FormBuilder::Template

## NAME
    [CGI::FormBuilder::Template](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate/markdown) - Template adapters for FormBuilder

## SYNOPSIS
        # Define a template engine

        package [CGI::FormBuilder::Template::Whatever](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate%3A%3AWhatever/markdown);
        use base '[Whatever::Template::Module](https://www.chedong.com/phpMan.php/perldoc/Whatever%3A%3ATemplate%3A%3AModule/markdown)';

        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](https://www.chedong.com/phpMan.php/perldoc/Whatever%3A%3ATemplate%3A%3AModule/markdown)->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
        }

## DESCRIPTION
    This documentation describes the usage of FormBuilder templates, as well as how to write your
    own template adapter.

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

        my $form = [CGI::FormBuilder](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder/markdown)->new(
                        template => 'filename.tmpl'
                   );

    This example points to a filename that contains an "[HTML::Template](https://www.chedong.com/phpMan.php/perldoc/HTML%3A%3ATemplate/markdown)" compatible template to use
    to layout the HTML. You can also specify the "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 "[HTML::Template](https://www.chedong.com/phpMan.php/perldoc/HTML%3A%3ATemplate/markdown)" with something like the following:

        my $form = [CGI::FormBuilder](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder/markdown)->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 "Template Toolkit". A minimal configuration would look like this:

        my $form = [CGI::FormBuilder](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder/markdown)->new(
                        fields => \@fields,
                        template => {
                            type => 'TT2',      # use Template Toolkit
                            template => 'form.tmpl',
                        },
                   );

    The "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](https://www.chedong.com/phpMan.php/perldoc/HTML%3A%3ATemplate/markdown)
        Text    -  [Text::Template](https://www.chedong.com/phpMan.php/perldoc/Text%3A%3ATemplate/markdown)
        TT2     -  Template Toolkit
        Fast    -  [CGI::FastTemplate](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFastTemplate/markdown)
        CGI_SSI -  [CGI::SSI](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASSI/markdown)

    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 "new()" and "render()" routines used. For example:

        my $form = [CGI::FormBuilder](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder/markdown)->new(
                        fields => \@fields,
                        template => {
                            type => '[My::Template::Module](https://www.chedong.com/phpMan.php/perldoc/My%3A%3ATemplate%3A%3AModule/markdown)',
                            template => 'form.tmpl',
                        },
                   );

    All other options besides "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 "SEE ALSO".

## 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 "render()". This sub will be called
    by FormBuilder when "$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 "[HTML::Template](https://www.chedong.com/phpMan.php/perldoc/HTML%3A%3ATemplate/markdown)"
    style template:

        # This file is My/HTML/Template.pm
        package [My::HTML::Template](https://www.chedong.com/phpMan.php/perldoc/My%3A%3AHTML%3A%3ATemplate/markdown);

        use [CGI::FormBuilder::Template::HTML](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate%3A%3AHTML/markdown);
        use base '[CGI::FormBuilder::Template::HTML](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate%3A%3AHTML/markdown)';

        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 FormBuilder:

        use [CGI::FormBuilder](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder/markdown);
        use [My::HTML::Template](https://www.chedong.com/phpMan.php/perldoc/My%3A%3AHTML%3A%3ATemplate/markdown);   # your module

        my $tmpl = [My::HTML::Template](https://www.chedong.com/phpMan.php/perldoc/My%3A%3AHTML%3A%3ATemplate/markdown)->new;

        my $form = [CGI::FormBuilder](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder/markdown)->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.

## SEE ALSO
    [CGI::FormBuilder](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder/markdown), [CGI::FormBuilder::Template::HTML](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate%3A%3AHTML/markdown), [CGI::FormBuilder::Template::Text](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate%3A%3AText/markdown),
    [CGI::FormBuilder::Template::TT2](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate%3A%3ATT2/markdown), [CGI::FormBuilder::Template::Fast](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate%3A%3AFast/markdown),
    [CGI::FormBuilder::Template::CGI_SSI](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate%3A%3ACGISSI/markdown)

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

## AUTHOR
    Copyright (c) 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.

