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

HTML::Template, TMPL_LOOP problem

by Lori713 (Pilgrim)
on Dec 10, 2003 at 13:46 UTC ( [id://313705]=perlquestion: print w/replies, xml ) Need Help??

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

I would like some help with the following. I've done several Super Searches, read through lots of examples, but I'm still stumped (probably not seeing the forest for the trees). I receive an error message when trying to run my radio.pl program:

HTML::Template->output() : fatal error in loop output : HTML::Template : Attempt to set nonexistent parameter '6' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params => 1) at /usr/local/lib/perl5.8.0/site_perl/HTML/Template.pm line 2905 at /local/www/htdocs/scripts/campus/radio.pl line 31

For the life of me, I can't figure out why it's not happy. I can add or subtract key/value pairs, and the error message line reference just moves up/down accordingly. Any help would be appreciated! (I've cut this example code down to a bare minimum; the original HTML code without TMPL_LOOP worked okay but was lengthy with lots of repetition. Given my customers' penchant for changing stuff, I like the idea of control tables so I have to make changes in only ONE place in my code). Thanks!

radio.pl

#!/usr/local/bin/perl5_8 use strict; use HTML::Template; $|=1; print "content-type: text/html\n\n"; my $template = HTML::Template->new( filename => 'radio.tmpl' ); ############################### Begin Section ###################### #Control table used to generate report radio buttons, rpt titles... #template vars: key->Rpt no. (rpt_no); value->Rpt Descr (rpt_descr) my %rpt_tbl = ( '1' => 'Fiscal Year-to-Date Financial Information' ,'2' => 'QTD/PTD Financial Information' ,'3' => 'Cash Balance' ,'4' => 'Financial Status' ,'5' => 'Financial Summary Status' ,'6' => 'Summary by Phase' ,'7' => 'Summary by Segment' ); my (@radio_loop, $key); foreach $key ( sort keys %rpt_tbl ) { $rpt_tbl{rpt_no} = $key; $rpt_tbl{rpt_descr} = $rpt_tbl{$key}; push(@radio_loop, \%rpt_tbl); } #+++++++++++++++++++++++++++++++ End Section ++++++++++++++++++++++++ $template->param(radio=>\@radio_loop); print $template->output();
radio.tmpl
<html> <head> <title>NCSU Campus Financials Reporting</title> <meta http-equiv="Pragma" content="no-cache"> <style type="text/css"> td.welc { font: bold 10pt "Arial"; color:black; text-align:le +ft; } </style> </head> <form name="frm_mainmenu" method="post" action="nc_rpt_summary.pl"> <table border="0" width="100%" summary="Input Form"> <tr> <td class=welc width="10%" rowspan="7" valign="top">Step 1:</td> <td class=welc width="20%" rowspan="7" valign="top">Select a Report</t +d> <td class=welc width="70%" id="rpt1form"> <TMPL_LOOP NAME=radio> <input type="RADIO" name="rpt_id" value="<TMPL_VAR NAME=rpt_no>_su +mm" onclick="1" onfocus='frm_mainmenu.rptname.value="Report <TMPL_VAR NAME= +rpt_no>";'> <span style="cursor:pointer;cursor:hand" onclick='window.open("../c2/descrs.html#Rpt<TMPL_VAR NAME=r +pt_no>Descr", "myWindow", "width=650,height=600,top=10,left=10,sc +rollbars=yes");'> &nbsp;&nbsp;Report <TMPL_VAR NAME=rpt_no> - <TMPL_VAR NAME= +rpt_descr> </span></td> <tr> <td class=welc> </TMPL_LOOP> &nbsp;</td></tr></table></form> </body></html>

Lori

Replies are listed 'Best First'.
Re: HTML::Template, TMPL_LOOP problem
by LTjake (Prior) on Dec 10, 2003 at 14:18 UTC

    Data::Dumper is your friend. Here's what it gives me for your original code:

    $VAR1 = [ { '6' => 'Summary by Phase', 'rpt_descr' => 'Summary by Segment', '3' => 'Cash Balance', '7' => 'Summary by Segment', '2' => 'QTD/PTD Financial Information', 'rpt_no' => '7', '1' => 'Fiscal Year-to-Date Financial Information', '4' => 'Financial Status', '5' => 'Financial Summary Status' }, $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0] ];

    Not what you want!

    By changing your loop to:

    foreach $key ( sort keys %rpt_tbl ) { push( @radio_loop, +{ rpt_no => $key, rpt_descr => $rpt_tbl{ $key +} } ); }

    It gives me:

    $VAR1 = [ { 'rpt_descr' => 'Fiscal Year-to-Date Financial Information' +, 'rpt_no' => '1' }, { 'rpt_descr' => 'QTD/PTD Financial Information', 'rpt_no' => '2' }, { 'rpt_descr' => 'Cash Balance', 'rpt_no' => '3' }, { 'rpt_descr' => 'Financial Status', 'rpt_no' => '4' }, { 'rpt_descr' => 'Financial Summary Status', 'rpt_no' => '5' }, { 'rpt_descr' => 'Summary by Phase', 'rpt_no' => '6' }, { 'rpt_descr' => 'Summary by Segment', 'rpt_no' => '7' } ];

    AND some output!

    HTH

    --
    "To err is human, but to really foul things up you need a computer." --Paul Ehrlich

Re: HTML::Template, TMPL_LOOP problem
by blokhead (Monsignor) on Dec 10, 2003 at 14:19 UTC
    You're adding stuff to the hash whose keys you are iterating over (%rpt_tbl) -- this is considered bad form. This hash has extra stuff (in terms of the HTML::Template vars) that the template doesn't like.

    I think what you really mean to do is this (or the equivalent map):

    for my $key (sort keys %rpt_tbl) { push @radio_loop, { rpt_no => $key, rpt_descr => $rpt_tbl{$key} }; }
    The way you had it, since you kept pushing a reference to the same hash onto @radio_loop, all the rpt_descr's and rpt_no's would have come out the same anyway.

    In the future, you can also avoid "this parameter name doesn't match any declarations in the template file" errors by setting die_on_bad_params to zero in the template declaration.

    blokhead

Re: HTML::Template, TMPL_LOOP problem
by amphiplex (Monk) on Dec 10, 2003 at 14:19 UTC
    Hi !

    The problem is the way you fill @radio_loop.
    This should work:
    foreach $key ( sort keys %rpt_tbl ) { push(@radio_loop, {rpt_no => $key, rpt_descr => $r +pt_tbl{$key}}); }

    ---- amphiplex

Re: HTML::Template, TMPL_LOOP problem
by Lori713 (Pilgrim) on Dec 10, 2003 at 14:37 UTC
    Thanks for the advice! I was making it harder than it had to be. It's now working beautimously!

    Lori713 now wanders off the read more about Data::Dumper.

    Lori

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-23 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found