http://www.perlmonks.org?node_id=244086

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

Hello again monks,

I am using HTML:Template to get data from a database, and put it into a form to be edited/updated. There is an outer TMPL_LOOP, and 7 inner TMPL_LOOPs. Everything works fine until I get to the 7th inner loop. At that point, I get no template processing and an error in my log that just says  at /usr/www/users/wiprayer/cgi-bin/edit_del.pl line 92.

If I delete the last TMPL_LOOP from the template, everything works fine, except that the last select list doesn't get populated.

Is this a limitation on the number of loops that can be used in the template? I Googled and I rtfm, but found no mention of a limitation on nesting loops, or the number of loops.

Thanks again for your help,
digger

Replies are listed 'Best First'.
Re: HTML:Template Limitation?
by digger (Friar) on Mar 18, 2003 at 18:21 UTC
    After playing around a little more, I noticed that my TMPL_LOOP label was the same as the name of the "select" list in the template. Changing the name of the TMPL_LOOP made everything work correctly.

    Another note I will be adding to my printed copy of the HTML:Template docs.

    This leads me to another question. If I find issues like this as I am working with modules, and I would like to contribute the info to the module documentation, how do I go about doing that?

    Thanks much,
    digger

      Every module's documentation includes contact information for the author. In my experience, just writing a polite email with a suggestion for a change should be enough.

      LAI

      __END__
Re: HTML:Template Limitation?
by LAI (Hermit) on Mar 18, 2003 at 18:22 UTC

    If I understand you correctly, you have nested <TMPL_LOOP>s seven or so levels deep, right? Well, I can't think of anything immediately wrong with that, but it would be helpful if you were to post your code for us to see. There's surely some little something you overlooked, and we can't help unless we can actually see what's going on.

    LAI

    __END__
Re: HTML:Template Limitation?
by defyance (Curate) on Mar 18, 2003 at 18:26 UTC
    I don't know of any limitations on looping with HTML::Template, can we see the offending code?

    -- Can't never could do anything, so give me and inch, I'll make it a mile.

Re: HTML:Template Limitation?
by digger (Friar) on Mar 18, 2003 at 20:27 UTC
    I wanted to post the offending section of the template here to see if others could confirm the side effect I experienced.
    <select name="type"> <tmpl_loop name="TYPE"> <option value="<tmpl_var name="n_event">" <t +mpl_var name="sel">> <tmpl_var name="t_event"> </option> </tmpl_loop> </select>

    If I change the TMPL_LOOP name to ETYPE, everthing works as expected. It appears that if the TMPL_LOOP has the same name as the select, some type of error occurs, but the error log doesn't show anything definite.

    Am I off base in my interpretation?

    --digger

      It shouldn't matter to HTML::Template what your select's name is. For consistancy I often name my form names, tmpl names all the same as my database column names and I've never had a problem with it.

      From your small snippet of code I can't really spot an error, it could be in your perl code itself. at any rate I'd suggest joining the HTML-Template-Users list (html-template-users@lists.sourceforge.net) and asking your question there. The author frequents the list and there are lots of other smart people (smarter than me) that would probably be able to tell you.

      Lobster Aliens Are attacking the world!

      Am I off base in my interpretation?
      I suspect so, but it's hard to say without a better look at the actual code in context that produces the error. I tried this template fragment out with some basic code to fake up the data, and got no errors (code and output below). Assuming that you can run this code without errors, can you try to find and post the shortest piece of code and template you can come up with that does produce the error you're seeing?

      #!/usr/bin/perl use strict; use warnings; use Carp 'verbose'; use HTML::Template; my $temp_text = <<EOT; <select name="type"> <tmpl_loop name="TYPE"> <option value="<tmpl_var name="n_event">" <tmpl_var name +="sel">> <tmpl_var name="t_event"> </option> </tmpl_loop> </select> EOT my $tpl = HTML::Template->new(scalarref => \$temp_text); my @ary = map {n_event => $_, sel=>$_,t_event=>$_}, qw(foo bar baz cliche trope); $tpl->param(TYPE=>\@ary); print $tpl->output; __END__ <select name="type"> <option value="foo" foo> foo </option> <option value="bar" bar> bar </option> <option value="baz" baz> baz </option> <option value="cliche" cliche> cliche </option> <option value="trope" trope> trope </option> </select>



      If God had meant us to fly, he would *never* have given us the railroads.
          --Michael Flanders

Re: HTML:Template Limitation?
by digger (Friar) on Mar 19, 2003 at 14:20 UTC
    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
      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