in reply to how to define html::template for the following hash in perl/cgi

Not to be dense, but why do you need to re-write the hash? You did all the work to build it, why should your templating system not handle it? I used HTML::Template a few years ago and am actually surprised that this type of thing continues to be an issue. I can't help much with H::t but here is the TemplateToolkit foo to do it. (showing another available option)
[% FOREACH element = add.keys %] [% IF add.$element.type == "text" %] <input type="text" name="[% add.$element.name %]" value="[% add.$e +lement.default %]"> [% ELSIF add.$element.type == "dropdown" %] <select name="[% add.$element.name %]"> [% FOREACH option = add.$element.value %] <option value="[% option %]"> [% option %] </option> [% END %] </select> [% END %] <br> [% # put a line break between the form elements %] [% END %]
In your perl code you would just pass the add hash into the TemplateToolkit process routine.
  • Comment on Re: how to define html::template for the following hash in perl/cgi
  • Download Code

Replies are listed 'Best First'.
Re^2: how to define html::template for the following hash in perl/cgi
by srins (Sexton) on Nov 05, 2005 at 20:03 UTC
    hi thanks for your suggestion i tried to pass the hash of hashes i constructed
    as $template->process($file,%add) || die $template->error(); for my hash my %add=( hsh1=>{ name=>"name1 ", type =>"text", default=>" ", value=>[" "], entries=>"M" }, hsh2=>{ name=>"name2 ", type=>"dropdown", req=>"prc", default=>" ", value=>["value1","value2","value3"], entries=>" " } ); for your code [% FOREACH element = add.keys %] [% IF add.$element.type == "text" %] <input type="text" name="[% add.$element.name %]" value="[% add.$e +lement.default %]"> [% ELSIF add.$element.type == "dropdown" %] <select name="[% add.$element.name %]"> [% FOREACH option = add.$element.value %] <option value="[% option %]"> [% option %] </option> [% END %] </select> [% END %] <br> [% # put a line break between the form elements %] [% END %]
    but i get following error, Content-type: text/html Can't use string ("hsh2") as a HASH ref while "strict refs" in use at /usr/cisco/packages/perl/perl-5.8.6/lib/site_perl/5.8.6/sun4-solaris/Template/Service.pm line 79. i also tried to pass the reference as $template->process($file,\%add) || die $template->error(); but nothing is substituted in the html file.can u help me how to pass my hash to the html file or i need to change the above code.please help me in this regard. Thanks, srins.