Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Pull down menu using HTML::Template

by sara2005 (Scribe)
on Jan 31, 2007 at 01:17 UTC ( [id://597477] : perlquestion . print w/replies, xml ) Need Help??

sara2005 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I running out of ideas to create a pull down by reading a data structure ( Array of Hashes ) and would appreciate some help/ ideas from fellow monks.

Data Structure:

my @AoH = ( { Lead => "fred", Friend => "barney", Son => "", No => "1", }, { Lead => "george", Wife => "jane", Son => "elroy|sam|bobby", No => "2", } );

In the present form, I am able to display the 'Sons' in the input text field but I want to have a pulldown menu if an array element has more than one name in the 'Son' field . For example, the 2nd element in the array of hashses has "elroy|sam|bobby", which I would lke to have as a pulldown.

Thanks

The code:

#! /usr/bin/perl -wT use strict; use warnings; use CGI qw/:standard :delete_all :escapeHTML :html3 :all/; use HTML::Template; # Get the data as array of hashes my $show_vars = get_data( ); print header; my $template = HTML::Template->new( filename => 'temp.tmpl' ); $template->param( show_vars => $show_vars ); print $template->output(); # subroutine to create the data structure sub get_data{ my @AoH = ( { Lead => "fred", Friend => "barney", Son => "", No => "1", }, { Lead => "george", Wife => "jane", Son => "elroy|sam|bobby", No => "2", } ); return ( \@AoH ); }

Template file :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Example</title> </head> <body> <table cellpadding="0" cellspacing="0" border="0" align="center" > <tmpl_loop name="show_vars"> <tr><td><hr /></td></tr> <tr valign="top" align="center"> <td ><a><tmpl_var name="Lead"></a> </td> </tr> <tr><td><br /></td></tr> <tr><td > <a>Son: </a><input type="text" name="<tmpl_var name="No">_<tmpl_var na +me="Son">" value="<tmpl_var name="Son">" /> </td> </tr> <tr><td><br /></td></tr> <tr><td><hr /></td></tr> <!-- Commented to hide from display <tr><td > <a>Friend: </a><input type="text" name="<tmpl_var name="No">_<tmpl_var + name="Friend">" value="" /> </td> </tr> <tr><td > <a>Wife: </a><input type="text" name="<tmpl_var name="No">_<tmpl_var n +ame="Wife">" value="" /> </td></tr> --> </tmpl_loop> </table> </body> </html>

Replies are listed 'Best First'.
Re: Pull down menu using HTML::Template
by bradcathey (Prior) on Jan 31, 2007 at 03:08 UTC

    Well, for starters, you are not structuring your data efficiently. You might take a lesson from the database concept of normalization. In a nutshell, when you are trying to represent more than one value in a variable (in this case, "sons") you should be seeing a red flag, or in Perlish terms, more hashes.

    You are probably looking at a loop within a loop, using a more complete structure of an AoHoAoH's (think that's right), the outer loop being the "adults" and the inner loop being the "sons." Also, H::T requires a reference to an AoH for loops. Oh, and one more thing, your keys should be the same in each element of the array--below I show both Wife and Friend in each. Otherwise there's no way for the loop to differentiate between the two in the HTML (no logic available unless you are using H::T::Expr)

    my $AoH = [ { 'Lead' => 'fred', 'Wife' => 'wilma', 'Friend' => 'barney', 'Son' => [ { 'name' => 'mike'}, { 'name' => 'jon' }, { 'name' => 'bill'} ], 'No' => '1', }, { 'Lead' => 'george', 'Wife' => 'jane', 'Friend' => 'sam', 'Son' => [ { 'name' => 'elroy'}, { 'name' => 'sam' }, { 'name' => 'bobby'} ], 'No' => '1', } ]; HTML: <tmpl_loop parents> <tmpl_var Lead> <tmpl_var Wife> <tmpl_var Friend> <tmpl_loop Son> <select> <option value="<tmpl_var Name>"><tmpl_var Name></option> </select> </tmpl_loop> </tmpl_loop>

    Anyway, see the HTML::Template docs for all the glorious details.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

      Works fine!!!

      I modified the code to create the data structure as suggested in your example.

      Thanks a lot.

Re: Pull down menu using HTML::Template
by jesuashok (Curate) on Jan 31, 2007 at 02:26 UTC
    I am able to display the 'Sons' in the input text field but I want to have a pulldown menu if an array element has more than one name in the 'Son' field

    Whether the array has single/multiple/none elements, directly you can create a pulldown menu instead of having a text box.

      Can you explain how that can be done?