{
    "mode": "perldoc",
    "parameter": "CGI::FormBuilder::Template::TT2",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate%3A%3ATT2/json",
    "generated": "2026-06-10T02:32:04Z",
    "synopsis": "my $form = CGI::FormBuilder->new(\nfields   => \\@fields,\ntemplate => {\ntype => 'TT2',\ntemplate => 'form.tmpl',\nvariable => 'form',\n}\n);",
    "sections": {
        "NAME": {
            "content": "CGI::FormBuilder::Template::TT2 - FormBuilder interface to Template Toolkit\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "my $form = CGI::FormBuilder->new(\nfields   => \\@fields,\ntemplate => {\ntype => 'TT2',\ntemplate => 'form.tmpl',\nvariable => 'form',\n}\n);\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This engine adapts FormBuilder to use \"Template Toolkit\". To do so, specify the \"template\"\noption as a hashref which includes the \"type\" option set to \"TT2\" and the \"template\" option set\nto the name of the template you want processed. You can also add \"variable\" as an option (among\nothers) to denote the variable name that you want the form data to be referenced by:\n\nmy $form = CGI::FormBuilder->new(\nfields => \\@fields,\ntemplate => {\ntype => 'TT2',\ntemplate => 'userinfo.tmpl',\nvariable => 'form',\n}\n);\n\nThe following methods are provided (usually only used internally):\n\nengine\nReturns a reference to the \"Template\" object\n\nprepare\nReturns a hash of all the fields ready to be rendered.\n\nrender\nUses the prepared hash and expands the template, returning a string of HTML.\n",
            "subsections": []
        },
        "TEMPLATES": {
            "content": "The template might look something like this:\n\n<html>\n<head>\n<title>[% form.title %]</title>\n[% form.jshead %]\n</head>\n<body>\n[% form.start %]\n<table>\n[% FOREACH field = form.fields %]\n<tr valign=\"top\">\n<td>\n[% field.required\n? \"<b>$field.label</b>\"\n: field.label\n%]\n</td>\n<td>\n[% IF field.invalid %]\nMissing or invalid entry, please try again.\n<br/>\n[% END %]\n\n[% field.field %]\n</td>\n</tr>\n[% END %]\n<tr>\n<td colspan=\"2\" align=\"center\">\n[% form.submit %] [% form.reset %]\n</td>\n</tr>\n</table>\n[% form.end %]\n</body>\n</html>\n\nBy default, the Template Toolkit makes all the form and field information accessible through\nsimple variables.\n\n[% jshead %]  -  JavaScript to stick in <head>\n[% title  %]  -  The <title> of the HTML form\n[% start  %]  -  Opening <form> tag and internal fields\n[% submit %]  -  The submit button(s)\n[% reset  %]  -  The reset button\n[% end    %]  -  Closing </form> tag\n[% fields %]  -  List of fields\n[% field  %]  -  Hash of fields (for lookup by name)\n\nYou can specify the \"variable\" option to have all these variables accessible under a certain\nnamespace. For example:\n\nmy $form = CGI::FormBuilder->new(\nfields => \\@fields,\ntemplate => {\ntype => 'TT2',\ntemplate => 'form.tmpl',\nvariable => 'form'\n},\n);\n\nWith \"variable\" set to \"form\" the variables are accessible as:\n\n[% form.jshead %]\n[% form.start  %]\netc.\n\nYou can access individual fields via the \"field\" variable.\n\nFor a field named...  The field data is in...\n--------------------  -----------------------\njob                   [% form.field.job   %]\nsize                  [% form.field.size  %]\nemail                 [% form.field.email %]\n\nEach field contains various elements. For example:\n\n[% myfield = form.field.email %]\n\n[% myfield.label    %]  # text label\n[% myfield.field    %]  # field input tag\n[% myfield.value    %]  # first value\n[% myfield.values   %]  # list of all values\n[% myfield.option   %]  # first value\n[% myfield.options  %]  # list of all values\n[% myfield.required %]  # required flag\n[% myfield.invalid  %]  # invalid flag\n\nThe \"fields\" variable contains a list of all the fields in the form. To iterate through all the\nfields in order, you could do something like this:\n\n[% FOREACH field = form.fields %]\n<tr>\n<td>[% field.label %]</td> <td>[% field.field %]</td>\n</tr>\n[% END %]\n\nIf you want to customise any of the Template Toolkit options, you can set the \"engine\" option to\ncontain a reference to an existing \"Template\" object or hash reference of options which are\npassed to the \"Template\" constructor. You can also set the \"data\" item to define any additional\nvariables you want accesible when the template is processed.\n\nmy $form = CGI::FormBuilder->new(\nfields => \\@fields,\ntemplate => {\ntype => 'TT2',\ntemplate => 'form.tmpl',\nvariable => 'form',\nengine   => {\nINCLUDEPATH => '/usr/local/tt2/templates',\n},\ndata => {\nversion => 1.23,\nauthor  => 'Fred Smith',\n},\n},\n);\n\nFor further details on using the Template Toolkit, see \"Template\" or\n<http://www.template-toolkit.org>\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "CGI::FormBuilder, CGI::FormBuilder::Template, Template\n",
            "subsections": []
        },
        "REVISION": {
            "content": "$Id: TT2.pm 100 2007-03-02 18:13:13Z nwiger $\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Copyright (c) Nate Wiger <http://nateware.com>. All Rights Reserved.\n\nTemplate Tookit support is largely due to a huge patch from Andy Wardley.\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::TT2 - FormBuilder interface to Template Toolkit",
    "flags": [],
    "examples": [],
    "see_also": []
}