{
    "mode": "perldoc",
    "parameter": "CGI::FormBuilder::Template",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate/json",
    "generated": "2026-06-13T01:09:12Z",
    "synopsis": "# Define a template engine\npackage CGI::FormBuilder::Template::Whatever;\nuse base 'Whatever::Template::Module';\nsub new {\nmy $self  = shift;\nmy $class = ref($self) || $self;\nmy %opt   = @;\n# override some options\n$opt{somesetting} = 0;\n$opt{anothervar}  = 'Some Value';\n# instantiate the template engine\n$opt{engine} = Whatever::Template::Module->new(%opt);\nreturn bless \\%opt, $class;\n}\nsub render {\nmy $self = shift;\nmy $form = shift;   # only arg is form object\n# grab any manually-set template params\nmy %tmplvar = $form->tmplparam;\n# example template manipulation\nmy $html = $self->{engine}->dotemplate(%tmplvar);\nreturn $html;       # scalar HTML is returned\n}",
    "sections": {
        "NAME": {
            "content": "CGI::FormBuilder::Template - Template adapters for FormBuilder\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "# Define a template engine\n\npackage CGI::FormBuilder::Template::Whatever;\nuse base 'Whatever::Template::Module';\n\nsub new {\nmy $self  = shift;\nmy $class = ref($self) || $self;\nmy %opt   = @;\n\n# override some options\n$opt{somesetting} = 0;\n$opt{anothervar}  = 'Some Value';\n\n# instantiate the template engine\n$opt{engine} = Whatever::Template::Module->new(%opt);\n\nreturn bless \\%opt, $class;\n}\n\nsub render {\nmy $self = shift;\nmy $form = shift;   # only arg is form object\n\n# grab any manually-set template params\nmy %tmplvar = $form->tmplparam;\n\n# example template manipulation\nmy $html = $self->{engine}->dotemplate(%tmplvar);\n\nreturn $html;       # scalar HTML is returned\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This documentation describes the usage of FormBuilder templates, as well as how to write your\nown template adapter.\n\nThe template engines serve as adapters between CPAN template modules and FormBuilder. A template\nengine is invoked by using the \"template\" option to the top-level \"new()\" method:\n\nmy $form = CGI::FormBuilder->new(\ntemplate => 'filename.tmpl'\n);\n\nThis example points to a filename that contains an \"HTML::Template\" compatible template to use\nto layout the HTML. You can also specify the \"template\" option as a reference to a hash,\nallowing you to further customize the template processing options, or use other template\nengines.\n\nFor example, you could turn on caching in \"HTML::Template\" with something like the following:\n\nmy $form = CGI::FormBuilder->new(\nfields => \\@fields,\ntemplate => {\nfilename => 'form.tmpl',\nsharedcache => 1\n}\n);\n\nAs mentioned, specifying a hashref allows you to use an alternate template processing system\nlike the \"Template Toolkit\". A minimal configuration would look like this:\n\nmy $form = CGI::FormBuilder->new(\nfields => \\@fields,\ntemplate => {\ntype => 'TT2',      # use Template Toolkit\ntemplate => 'form.tmpl',\n},\n);\n\nThe \"type\" option specifies the name of the engine. Currently accepted types are:\n\nBuiltin -  Included, default rendering if no template specified\nDiv     -  Render form using <div> (no tables)\nHTML    -  HTML::Template\nText    -  Text::Template\nTT2     -  Template Toolkit\nFast    -  CGI::FastTemplate\nCGISSI -  CGI::SSI\n\nIn addition to one of these types, you can also specify a complete package name, in which case\nthat module will be autoloaded and its \"new()\" and \"render()\" routines used. For example:\n\nmy $form = CGI::FormBuilder->new(\nfields => \\@fields,\ntemplate => {\ntype => 'My::Template::Module',\ntemplate => 'form.tmpl',\n},\n);\n\nAll other options besides \"type\" are passed to the constructor for that templating system\nverbatim, so you'll need to consult those docs to see what all the different options do. Skip\ndown to \"SEE ALSO\".\n",
            "subsections": []
        },
        "SUBCLASSING TEMPLATE ADAPTERS": {
            "content": "In addition to the above included template engines, it is also possible to write your own\nrendering module. If you come up with something cool, please let the mailing list know!\n\nTo do so, you need to write a module which has a sub called \"render()\". This sub will be called\nby FormBuilder when \"$form->render\" is called. This sub can do basically whatever it wants, the\nonly thing it has to do is return a scalar string which is the HTML to print out.\n\nThis is actually not hard. Here's a simple adapter which would manipulate an \"HTML::Template\"\nstyle template:\n\n# This file is My/HTML/Template.pm\npackage My::HTML::Template;\n\nuse CGI::FormBuilder::Template::HTML;\nuse base 'CGI::FormBuilder::Template::HTML';\n\nsub render {\nmy $self = shift;    # class object\nmy $form = shift;    # $form as only argument\n\n# the template object (engine) lives here\nmy $tmpl = $self->engine;\n\n# setup vars for our fields (objects)\nfor ($form->field) {\n$tmpl->param($ => $->value);\n}\n\n# render output\nmy $html = $tmpl->output;\n\n# return scalar;\nreturn $html;\n}\n1;  # close module\n\nThen in FormBuilder:\n\nuse CGI::FormBuilder;\nuse My::HTML::Template;   # your module\n\nmy $tmpl = My::HTML::Template->new;\n\nmy $form = CGI::FormBuilder->new(\nfields   => [qw(name email)],\nheader   => 1,\ntemplate => $tmpl   # pass template object\n);\n\n# set our company from an extra CGI param\nmy $co = $form->cgiparam('company');\n$tmpl->engine->param(company => $co);\n\n# and render like normal\nprint $form->render;\n\nThat's it! For more details, the best thing to do is look through the guts of one of the\nexisting template engines and go from there.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "CGI::FormBuilder, CGI::FormBuilder::Template::HTML, CGI::FormBuilder::Template::Text,\nCGI::FormBuilder::Template::TT2, CGI::FormBuilder::Template::Fast,\nCGI::FormBuilder::Template::CGISSI\n",
            "subsections": []
        },
        "REVISION": {
            "content": "$Id: Template.pm 97 2007-02-06 17:10:39Z nwiger $\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Copyright (c) Nate Wiger <http://nateware.com>. All Rights Reserved.\n\nThis module is free software; you may copy this under the terms of the GNU General Public\nLicense, or the Artistic License, copies of which should have accompanied your Perl kit.\n",
            "subsections": []
        }
    },
    "summary": "CGI::FormBuilder::Template - Template adapters for FormBuilder",
    "flags": [],
    "examples": [],
    "see_also": []
}