Re: [htmltmpl] TMPL_VAR inside a TMPL_VAR
Brought to you by:
samtregar
From: LDT <per...@ya...> - 2004-01-13 13:56:17
|
Thanks for the reply, Ron. I'll try to answer your questions. I ended up just using a TMPL_INCLUDE to handle it since it was taking me so long to figure it out (and the reports are due 2-1-04, and this is just the first one). However, I really appreciate your taking the time to explain to me why it didn't work. That helps me (and future folks searching the archives) to understand how things like TMPL_VAR work. I already had global_vars set to 1 (I think I had snipped that out in my first post - my bad). My goal was the take a scalar ($cell_fmt) and pass it to the main template. However, the $cell_fmt scalar had a TMPL_VAR inside it, and I learned that H:T won't interpret it in "layers" (which prompted my initial post). The "big_tmpl" stuff was something I was trying that someone else had suggested but I didn't understand him. I ended up deleting all the "big_tmpl" stuff, and going with the TMPL_INCLUDE. I had tried to add the $cell_fmt to my @loop_data (which is fed into the TMPL_LOOP on the main template), but it didn't like that either (undoubtedly it was the way I was trying to add it). I am curious about this piece of your code: filehandle => \*DATA. What does this do? The parts I understand are that you're setting up file handle, the "\" is a reference, and the "DATA" is referenced to the section below. My guess would be that this is how you're passing the formatting stuff to the template. Is that right? I'm not trying to correlate the two arrays in the template. The @loop_attr actually goes into one of the main template's TMPL_INCLUDEs (and then into it's own TMPL_LOOP) and really has nothing to do with @loop_data (they each have their own TMPL_LOOP). The final .pl code is about 450 lines; the templates all total are about 100 lines. If you want to see it, let me know and I'll send it. Thanks again for taking the time to help me! Lori Thompson Ron Mahoney <rma...@in...> wrote: Lori: The idea behind HTML::Template is that you pass it a reference to a hash and it uses the keys of the hash as the namespace for that template. Additionally, for TMPL_LOOP variables you pass a reference to an array as the value to one of the keys in the hash reference that your passing to the template. The new problem in your template is that you have not passed a value for all the variables in the template $tmpl_main. The default behavior for HTML::Template is to complain when a value for a template variable is not found in the hash reference that has been passed to it. This behavior can be turned off with die_on_bad_params=>0 passed to HTML::Temaplte->new. Additionally, inside a TMPL_LOOP, by default, other paramters are not visible. This can be changed by passing global_vars=>1 to new(). Also, it looks like you got things a bit confused between your two templates $tmpl_main and $big_tmpl. Now, I'm not quite sure of everything your trying to accomplish, but I think that you probably don't need the separate template for the cell format. And it looks like your trying to correlate the @loop_data and @loop_attrs arrays, which you can't in HTML::Template. You would have to put the data for each row in one hash reference and then all the rows in one array reference (for the TMPL_LOOP var). So for your code above this would work: #!/usr/bin/perl -w use strict; use HTML::Template; my $color1='white'; my $color2='red'; my @loop_data = ( { col_cfte => 'col_cfte_val1', col_cbud => 'col_cbud_val1', trclass=>'class1', bgcolor=>"$color1", }, { col_cfte => 'col_cfte_val2', col_cbud => 'col_cbud_val2', trclass=>'class1', bgcolor=>"$color2", }, { col_cfte => 'col_cfte_val3', col_cbud => 'col_cbud_val3', trclass=>'class1', bgcolor=>"$color1", }, ); my $big_tmpl = HTML::Template->new( die_on_bad_params=>0, global_vars=>1, filehandle => \*DATA, ); $big_tmpl->param( { title_bar => 'Rpt 1', hdr_asofdt => 'Today', attr_fyr => 'someval', passdata => \@loop_data, } ); print $big_tmpl->output; __DATA__ bgcolor=> style="cursor:pointer;cursor:hand;" title="Click to drill down" onmouseover="style.background='#CCCCFF';" onmouseout ="style.background='';" > style="cursor:pointer;cursor:hand;" title="Click to drill down" onmouseover="style.background='#CCCCFF';" onmouseout ="style.background='';" > Unless there is something else you are trying to accomplish here. Regards, Ron --------------------------------- Do you Yahoo!? Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes |