Formbuilder Template Variables

44 views
Skip to first unread message

Glenn

unread,
Dec 21, 2016, 2:04:21 PM12/21/16
to perl-formbuilder

I am trying to figure out how to add non formbuilder field values to my HTML Template. Below is an example of what I am trying to accomplish with $User.
Any suggestions appreciated!

TIA
Glenn

perl.pl:

my $User = $ENV{REMOTE_USER};
my @fields = qw(f1 f2 f3 f4);
my $form = CGI::FormBuilder->new(
                 method => 'get',
                 fields => \@fields,
                 template => {filename => template.tmpl',
                 param=>{user =>$User }}

print "Content-Type: text/html\n\n";
        print $form->render(header => 0);


template.tmpl:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">   
</head>
<body>

<h1>Hello <tmpl_var name=user></h1>

<tmpl_var form-start>
F1:<tmpl_var field-f1><br>
F2:<tmpl_var field-f2><br>
F3:<tmpl_var field-f3><br>
F4:<tmpl_var field-f4><br>

<tmpl_var form-submit> <br><br>
<tmpl_var form-end>
</body>
</html>

Glenn

unread,
Dec 23, 2016, 1:05:02 PM12/23/16
to perl-formbuilder
Solved.  Thought I would post my solution for anyone else searching.

From the docs:

tmpl_param()

This allows you to manipulate template parameters directly. Extending the above example:

    my $form = CGI::FormBuilder->new(template => 'some.tmpl');

    my $company = $form->cgi_param('company');
    $form->tmpl_param(company => $company);

Then, in your template:

    Hello, <tmpl_var company> employee!
    <p>
    Please fill out this form:
    <tmpl_var form-start>
    <!-- etc... -->
For really precise template control, you can actually create your own template object and then pass it directly to FormBuilder. See the CGI::FormBuilder::Template manpage for more details

Glenn
Reply all
Reply to author
Forward
0 new messages