Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: HTML:Template Limitation?

by digger (Friar)
on Mar 19, 2003 at 14:20 UTC ( [id://244339]=note: print w/replies, xml ) Need Help??


in reply to HTML:Template Limitation?

I wasn't able to post the actual code sooner because I didn't have my laptop where the code was. I know this post is a bit late, so I hope someone who knows more than me sees this.
sub build_evntype { my @event_entries; my @event_list=("Event1", "Event2"); my $p_event = shift; for (my $i=0; $i<=$#event_list; $i++) { my $event_data={}; $$event_data{"t_event"}=$event_list[$i]; $$event_data{"n_event"}=$i+1; if ($i+1 == $p_event) { $$event_data{"sel"} = "selected"; } push (@event_entries, $event_data); } return \@event_entries; }
I am using the same type of code to build 6 other select lists in my template, and they all work fine.

To fix my problem, I didn't touch the code, I just changed the name of the TMPL_LOOP, and it worked. I agree that it doesn't make sense, I am only interpreting the information I have at hand.

Thanks for all your insight,
digger

Replies are listed 'Best First'.
(jeffa) 2Re: HTML:Template Limitation?
by jeffa (Bishop) on Mar 19, 2003 at 15:08 UTC
    Congradulations! You just re-invented CGI.pm's popup_menu(). samtregar (the author of HTML::Template) recommends using CGI.pm for this very thing. Here is a quick example (with validation!):
    use strict; use warnings; use HTML::Template; use CGI qw(:standard); my @color = qw(red green blue black purple orange); my %valid = map {$_ => 1} @color; my $tmpl = HTML::Template->new(filehandle => *DATA); my $color = param('color'); my $select = popup_menu( -name => 'color', -values => [@color], -default => $color, ); $tmpl->param(error => 1) unless $valid{$color}; $tmpl->param( colors => $select, color => $color, ); print header, $tmpl->output; __DATA__ <form> <p> Pick a color: <tmpl_var colors> </p> <input type="submit"> </form> <tmpl_if color> <p><hr/></p> <tmpl_unless error> You picked <span style="color: <tmpl_var color>;"><tmpl_var color></span> <tmpl_else> Invalid choice, "<tmpl_var color>" </tmpl_unless> </tmpl_if>
    Now then, you say that you use 'the same type of code to build 6 other select lists' ... why not use one function to handle them all? ;) (and don't say because they have different 'business rules' ... if that's the case, do the extra specific stuff first, then call popup_menu()). Happy coding to ya. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Yeah, I read about that in the FAQ section of the HTML:Template docs. I kinda thought doing this the "hard way" would be a good way to learn about references and passing refs around in a program. It was a good way to learn, but the code duplication probably sends good programmers into fits. I am still learning these things though, so I don't include myself in the "good programmer"group.

      Now that I accomplished my goal, I will probably go back and rewrite the code using the CGI.pm methods.

      Still wondering about the error mesage I was getting, and why changing the TMPL_LOOP label solved the problem.

      Thanks alot,
      digger

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://244339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found