Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

CGI to Dancer popup_menu

by adriang (Sexton)
on Jan 15, 2015 at 14:03 UTC ( [id://1113330]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All

I'm converting a CGI site to Dancer and I need to convert the FORM with popup_menu that have HASH as labels:

$cgi->start_form({-name=>'box_form',-target=>"_new"}), $cgi->font({-'size'=>2,-'color'=>"#2E2E2E",-'face'=>"arial"}),$cgi +->b(), $cgi->table({-width=>"600",-border=>"1", -cellpadding=>"5", -cells +pacing=>"2", -align=>"center",-bgcolor=>"#FFFFFF",-bordercolor=>"#6E6 +E6E"}, $cgi->Tr( $cgi->td({-align=>"center",-colspan=>"2"},$cgi->font({-'si +ze'=>4,-'color'=>"#2E2E2E",-'face'=>"arial"},"Query Done") ) ), $cgi->Tr({-align=>"right"}, $cgi->td( $cgi->popup_menu({-name=>"query",-values=>\@query_ +type_mainbox,-labels=>\%query_name_mainbox, -default=>"license"} ),"Q +uery Type"), $cgi->td( $cgi->popup_menu({-name=>"cust",-values=>\@cust_co +de_sorted,-labels=>\%cust_name} ),"Customer Name", $cgi->br(),$cgi->br(), $cgi->textfield({-size=>8,-name=>"free_cust"}), "C +ustomer Code") ), $cgi->Tr({-align=>"center"}, $cgi->td({-colspan=>"2"}, $cgi->submit(-name=>"Go")) ), ), $cgi->endform, ;


A good advice will be thankful how can I write the code into the template.tt file.
This is the route:

get '/' => sub { # Create customers list my $db = connect_db(); my $sth=$db->prepare("SELECT shortcut,nameheb FROM cust"); $sth->execute() or die "$DBI::errstr"; # Create customer HASH my %cust_name; my @cust_code; while(my ($shortcut,$name) = $sth->fetchrow_array()) { $cust_name{$shortcut}="$name"; } $sth->finish(); template 'index', { hash => \%cust_name }; };

Thanks in advance

Replies are listed 'Best First'.
Re: CGI to Dancer popup_menu
by choroba (Cardinal) on Jan 15, 2015 at 14:23 UTC
    You should create the template from the output of the CGI, not from the code.
    perl -MCGI=:standard -e 'print popup_menu({ -name => "query", -values => [1, 2, 3], -labels => {qw{ 2 label2 +1 label1 3 label3 }}, -default => 2 })' <select name="query" > <option value="1">label1</option> <option selected="selected" value="2">label2</option> <option value="3">label3</option> </select>

    Update

    So, using Template, the following seems to work:

    Template call:

    template 'index', { name => 'query', options => { label1 => 'Option 1', label2 => 'Option 2', label3 => 'Option 3', }, default => 'label2', };

    The template:

    <select name="<% name %>" > <%- FOR o IN options %> <option value="<% o.key %>" <%- IF o.key == default %> selected="selected" <% END -%> ><% o.value %></option> <%- END %> </select>
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      A good habit for Template code is escape all template vars. That way DB/User-input strings can be plain/arbitrary without risk of XSS attacks.

      E.g.: <% name | html %>
      Thanks very much for the quick replay, I will try it right away.
        Thanks again, it was a success.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1113330]
Approved by Corion
Front-paged by choroba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-28 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found