{
    "mode": "perldoc",
    "parameter": "CGI::FormBuilder::Template::Text",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AFormBuilder%3A%3ATemplate%3A%3AText/json",
    "generated": "2026-06-10T16:29:59Z",
    "synopsis": "my $form = CGI::FormBuilder->new(\nfields   => \\@fields,\ntemplate => {\ntype => 'Text',\ntemplate => 'form.tmpl',\nvariable => 'form',\n}\n);",
    "sections": {
        "NAME": {
            "content": "CGI::FormBuilder::Template::Text - FormBuilder interface to Text::Template\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "my $form = CGI::FormBuilder->new(\nfields   => \\@fields,\ntemplate => {\ntype => 'Text',\ntemplate => 'form.tmpl',\nvariable => 'form',\n}\n);\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This engine adapts FormBuilder to use \"Text::Template\". Usage is very similar to Template\nToolkit:\n\nmy $form = CGI::FormBuilder->new(\nfields => \\@fields,\ntemplate => {\ntype => 'Text',           # use Text::Template\ntemplate => 'form.tmpl',\n}\n);\n\nThe default options passed into \"Text::Template->new()\" with this calling form are:\n\nTYPE   => 'FILE'\nSOURCE => 'form.tmpl'\nDELIMITERS => ['<%','%>']\n\nAs these params are passed for you, your template will look very similar to ones used by\nTemplate Toolkit and \"HTML::Mason\" (the Text::Template default delimiters are \"{\" and \"}\", but\nusing alternative delimiters speeds it up by about 25%, and the \"<%\" and \"%>\" delimiters are\ngood, familiar-looking alternatives).\n\nThe following methods are provided (usually only used internally):\n\nengine\nReturns a reference to the \"Text::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": "<% $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\nNote that you refer to variables with a preceding \"$\", just like in Perl. Like Template Toolkit,\nyou can specify a variable to place fields under:\n\nmy $form = CGI::FormBuilder->new(\nfields => \\@fields,\ntemplate => {\ntype => 'Text',\ntemplate => 'form.tmpl',\nvariable => 'form'\n},\n);\n\nUnlike Template Toolkit, though, these will not be placed in OO-style, dot-separated vars.\nInstead, a hash will be created which you then reference:\n\n<% $form{jshead} %>\n<% $form{start}  %>\netc.\n\nAnd field data is in a hash-of-hashrefs format:\n\nFor a field named...  The field data is in...\n--------------------  -----------------------\njob                   <% $form{field}{job}   %]\nsize                  <% $form{field}{size}  %]\nemail                 <% $form{field}{email} %]\n\nSince \"Text::Template\" looks so much like Perl, you can access individual elements and create\nvariables like so:\n\n<%\nmy $myfield = $form{field}{email};\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->{options};    # list of all options\n$myfield->{required};   # required flag\n$myfield->{invalid};    # invalid flag\n$myfield->{error};      # error string if invalid\n%>\n\n<%\nfor my $field (@{$form{fields}}) {\n$OUT .= \"<tr>\\n<td>\" . $field->{label} . \"</td> <td>\"\n. $field->{field} . \"</td>\\n<tr>\";\n}\n%>\n\nIn addition, when using the engine option, you supply an existing Text::Template object or a\nhash of parameters to be passed to \"new()\". For example, you can ask for different delimiters\nyourself:\n\nmy $form = CGI::FormBuilder->new(\nfields => \\@fields,\ntemplate => {\ntype => 'Text',\ntemplate => 'form.tmpl',\nvariable => 'form',\nengine   => {\nDELIMITERS => [ '[@--', '--@]' ],\n},\ndata => {\nversion => 1.23,\nauthor  => 'Fred Smith',\n},\n},\n);\n\nIf you pass a hash of parameters, you can override the \"TYPE\" and \"SOURCE\" parameters, as well\nas any other \"Text::Template\" options. For example, you can pass in a string template with \"TYPE\n=> STRING\" instead of loading it from a file. You must specify both \"TYPE\" and \"SOURCE\" if doing\nso. The good news is this is trivial:\n\nmy $form = CGI::FormBuilder->new(\nfields => \\@fields,\ntemplate => {\ntype => 'Text',\nvariable => 'form',\nengine   => {\nTYPE => 'STRING',\nSOURCE => $string,\nDELIMITERS => [ '[@--', '--@]' ],\n},\ndata => {\nversion => 1.23,\nauthor  => 'Fred Smith',\n},\n},\n);\n\nIf you get the crazy idea to let users of your application pick the template file (strongly\ndiscouraged) and you're getting errors, look at the \"Text::Template\" documentation for the\n\"UNTAINT\" feature.\n\nAlso, note that \"Text::Template\"'s \"PREPEND => 'use strict;'\" option is not recommended due to\nthe dynamic nature for \"FormBuilder\". If you use it, then you'll have to declare each variable\nthat \"FormBuilder\" puts into your template with \"use vars qw($jshead' ... etc);\"\n\nIf you're really stuck on this, though, a workaround is to say:\n\nPREPEND => 'use strict; use vars qw(%form);'\n\nand then set the option \"variable => 'form'\". That way you can have strict Perl without too much\nhassle, except that your code might be exhausting to look at :-). Things like\n$form{field}{yourfieldname}{field} end up being all over the place, instead of the nicer short\nforms.\n\nFinally, when you use the \"data\" template option, the keys you specify will be available to the\ntemplate as regular variables. In the above example, these would be \"<% $version %>\" and \"<%\n$author %>\". And complex datatypes are easy:\n\ndata => {\nanArray => [ 1, 2, 3 ],\naHash => { orange => 'tangy', chocolate => 'sweet' },\n}\n\nThis becomes the following in your template:\n\n<%\n@anArray;    # you can use $myArray[1] etc.\n%aHash;      # you can use $myHash{chocolate} etc.\n%>\n\nFor more information, please consult the \"Text::Template\" documentation.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "CGI::FormBuilder, CGI::FormBuilder::Template, Text::Template\n",
            "subsections": []
        },
        "REVISION": {
            "content": "$Id: Text.pm 100 2007-03-02 18:13:13Z nwiger $\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Copyright (c) Nate Wiger <http://nateware.com>. All Rights Reserved.\n\nText::Template support is due to huge contributions by Jonathan Buhacoff. Thanks man.\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::Text - FormBuilder interface to Text::Template",
    "flags": [],
    "examples": [],
    "see_also": []
}