Thread: [htmltmpl] Templates with template loops within templates
Brought to you by:
samtregar
From: Eric F. <er...@dm...> - 2004-12-16 23:38:44
|
Hi, Great subject line huh? :) Ok we have a template. Inside that template we load in from a database a template with its own vars which makes use of a template loop. In that second template we have a var called URL_BASE which is the base path. It works inside the second template, except for within the template loop output. So imagine we already have template object: $template (it by the way has a var called URL_BASE also that I thought would be passed down to this inside template as well) my $shipping_temp = $config->getShipping_temp($self->param('DEFAULT_PRODUCT')); ## returns array ref my $ship_list_hash = $config->getShipping_list($instance,$dom_int); $template->param(SHIPPING => '1'); ## part of the top level template my $shipref = \$shipping_temp->[0]; my $ship_html = HTML::Template->new( scalarref => $shipref, die_on_bad_params =>0); my @loop_values = (); my $num_options = keys(%$ship_list_hash); foreach my $key( keys(%$ship_list_hash) ){ my %inside_hash = ('S_ICON' => $ship_list_hash->{$key}{'courier_icon_url'}, 'S_COURIER' => $ship_list_hash->{$key}{'courier_name'}, 'S_METHOD' => $ship_list_hash->{$key}{'courier_method'} , 'S_METHOD_ID' => $ship_list_hash->{$key}{'ship_method_id'}, 'S_COST' => $ship_list_hash->{$key}{'shipping_cost'} ); if ($shipping_temp->[1] eq $ship_list_hash->{$key}{'ship_method_id'}){ $inside_hash{'DEFAULT'} = 'Checked'; } if ($num_options < 2){ $inside_hash{'DEFAULT'} = 'Checked'; } push(@loop_values,\%inside_hash); } $ship_html->param(URL_BASE => $self->param('URL_BASE')); ## the problem var $ship_html->param(SHIP_LOOP => \@loop_values); my $tmpout = $ship_html->output(); Below is the raw HTML output produced, notice the spacer.gif has a full url where the ups_2D.gif which comes from the loop does not. I was a little confused to start off with because I thought the presence of the URL_BASE in the second template would be parsed as a result of being set in the original top level template. So not only does that not seem to be happening, but also when setting the var again in the second template it is not working inside the template loop. So my first question, is this the way HTML::Template should be working? I didn't expect this problem. The next question of course is how can I get this var to be parsed inside the loop? Using the current versions of both HTML::Template and CGI::Application Please let me know if this is enough info or too much, I wanted to give as much detail as I could because I found it hard to explain what I am doing with just words. Thanks, Eric <!--Start of Shipping type Section --> <TABLE WIDTH=700 class="bord" BORDER=0 CELLPADDING=2 CELLSPACING=0 align="center"> <TR> <TD class="bord" align="center"><span class="title">Shipping Method</span></TD> </TR> <TR> <TD> <!--second color table--> <TABLE WIDTH=700 class="main" BORDER=0 CELLPADDING=10 CELLSPACING=0 align="center"> <TR> <TD class="main" align="center"> <!--pieces table--> <TABLE WIDTH=95% BORDER=0 CELLPADDING=0 CELLSPACING=0 align="center"> <tr> <td><IMG SRC="https://www.host.com/htmltemp_consol/images/spacer.gif" WIDTH=10 HEIGHT=5></TD> </tr> <tr> <td> <table align="center"> <tr> <label for="ship4"> <TD width=15 align="right"><input type=radio id="ship4" name="ship_option" value="4" Checked></TD> <TD width=65 class="boxfields" align="center" valign="top"> <img src="images/shipping/ups_2D.gif" alt="LEM Domestic "><br><b>$10.00</b></TD> </label> </tr> </table> </td> </tr> <tr> <td><IMG SRC="https://www.host.com/htmltemp_consol/images/spacer.gif" WIDTH=10 HEIGHT=5></TD> </tr> </table> <!--end of pieces table--> </td> </tr> </table> <!--end of second color table--> </TD> </TR> </table> <!--end of Shipping type Section--> |
From: Eric F. <er...@dm...> - 2004-12-17 17:59:55
|
Hi, I think I understand what you are saying, but that was part of the confusion. The URL_BASE is used before the loop in the top level template.(Sorry I didn't show the entire thing) At least I think I understood what you said, that there is a bug where if the first time a var is referenced is in a loop it won't be replaced. If that is so, then that bug can't be the reason I am sorry to say :) Thanks very much for the reply, Eric >It sounds like you have hit the dreaded "top level param, not referenced >before use in >template loop, so I"ll just do nothing" bug... try this somewhere before >your first >TMPL_LOOP (say at the top of the file), in your first template: > > <!-- H::T BUG: <TMPL_VAR URL_BASE> --> > > which _should_ do absolutely nothing useful/harmful to the html > output. However, it will >result in H:T actually getting a reference to the URL_BASE variable. |
From: Mathew R. <mat...@re...> - 2004-12-16 23:54:39
|
Hi Eric, It sounds like you have hit the dreaded "top level param, not referenced = before use in template loop, so I'll just do nothing" bug... try this = somewhere before your first TMPL_LOOP (say at the top of the file), in = your first template: <!-- H::T BUG: <TMPL_VAR URL_BASE> --> which _should_ do absolutely nothing useful/harmful to the html output. = However, it will result in H:T actually getting a reference to the = URL_BASE variable. What you were expecting to happen (ie template variable used in some = child template, when being set at the top-level), is what most people = expect to happen... In my own templates, I have also hit this problem. This bug is a result of how H::T handles TMPL_LOOP invocations... it = basically does a recursive call of itself, passing the known params from = the previous instance, into this new invocation. Since the previous = invocation hasn't referenced the TMPL_VAR, that value isn't passed into = the TMPL_LOOP invocation. Hope this helps, Mathew ----- Original Message -----=20 From: "Eric Frazier" <er...@dm...> To: <htm...@li...> Sent: Friday, December 17, 2004 10:39 AM Subject: [htmltmpl] Templates with template loops within templates > Hi, >=20 > Great subject line huh? :) >=20 > Ok we have a template. > Inside that template we load in from a database a template with its = own=20 > vars which makes use of a template loop. > In that second template we have a var called URL_BASE which is the = base=20 > path. It works inside the second template, except for within the = template=20 > loop output. >=20 >=20 > So imagine we already have template object: $template (it by the way = has a=20 > var called URL_BASE also that I thought would be passed down to this = inside=20 > template as well) >=20 > my $shipping_temp =3D=20 > $config->getShipping_temp($self->param('DEFAULT_PRODUCT')); ## returns = > array ref > my $ship_list_hash =3D $config->getShipping_list($instance,$dom_int); >=20 > $template->param(SHIPPING =3D> '1'); ## part of the top level template >=20 > my $shipref =3D \$shipping_temp->[0]; >=20 > my $ship_html =3D HTML::Template->new( scalarref =3D> $shipref, > die_on_bad_params =3D>0); > my @loop_values =3D (); > my $num_options =3D keys(%$ship_list_hash); >=20 > foreach my $key( keys(%$ship_list_hash) ){ >=20 > my %inside_hash =3D ('S_ICON' =3D>=20 > $ship_list_hash->{$key}{'courier_icon_url'}, > 'S_COURIER' =3D> = $ship_list_hash->{$key}{'courier_name'}, > 'S_METHOD' =3D> = $ship_list_hash->{$key}{'courier_method'} , > 'S_METHOD_ID' =3D> = $ship_list_hash->{$key}{'ship_method_id'}, > 'S_COST' =3D>=20 > $ship_list_hash->{$key}{'shipping_cost'}=20 >=20 > ); >=20 > if ($shipping_temp->[1] eq=20 > $ship_list_hash->{$key}{'ship_method_id'}){ > $inside_hash{'DEFAULT'} =3D 'Checked'; > } >=20 > if ($num_options < 2){ > $inside_hash{'DEFAULT'} =3D 'Checked'; > } >=20 >=20 > push(@loop_values,\%inside_hash); >=20 > } > $ship_html->param(URL_BASE =3D> = $self->param('URL_BASE')); ##=20 > the problem var > $ship_html->param(SHIP_LOOP =3D> \@loop_values); >=20 >=20 > my $tmpout =3D $ship_html->output(); >=20 >=20 >=20 > Below is the raw HTML output produced, notice the spacer.gif has a = full url=20 > where the ups_2D.gif which comes from the loop does not. >=20 > I was a little confused to start off with because I thought the = presence of=20 > the URL_BASE in the second template would be parsed as a result of = being=20 > set in the original top level template. So not only does that not seem = to=20 > be happening, but also when setting the var again in the second = template it=20 > is not working inside the template loop. So my first question, is this = the=20 > way HTML::Template should be working? I didn't expect this problem. = The=20 > next question of course is how can I get this var to be parsed inside = the=20 > loop? >=20 > Using the current versions of both HTML::Template and CGI::Application >=20 > Please let me know if this is enough info or too much, I wanted to = give as=20 > much detail as I could because I found it hard to explain what I am = doing=20 > with just words. >=20 >=20 > Thanks, >=20 > Eric >=20 >=20 > <!--Start of Shipping type Section=20 > --> > <TABLE WIDTH=3D700 class=3D"bord" BORDER=3D0 CELLPADDING=3D2 = CELLSPACING=3D0=20 > align=3D"center"> > <TR>=20 >=20 > <TD class=3D"bord" align=3D"center"><span=20 > class=3D"title">Shipping Method</span></TD> > </TR>=20 >=20 > <TR>=20 >=20 > <TD>=20 >=20 > <!--second color=20 > table--> > <TABLE WIDTH=3D700 class=3D"main" BORDER=3D0 = > CELLPADDING=3D10 CELLSPACING=3D0 align=3D"center"> > <TR>=20 >=20 > <TD class=3D"main"=20 > align=3D"center"> > <!--pieces=20 > table--> > <TABLE WIDTH=3D95% BORDER=3D0 = CELLPADDING=3D0=20 > CELLSPACING=3D0 align=3D"center"> > <tr>=20 >=20 > <td><IMG=20 > SRC=3D"https://www.host.com/htmltemp_consol/images/spacer.gif" = WIDTH=3D10=20 > HEIGHT=3D5></TD> > </tr>=20 >=20 > <tr> > <td> > <table align=3D"center"> > <tr> >=20 >=20 >=20 > <label=20 > for=3D"ship4">=20 >=20 > <TD width=3D15 align=3D"right"><input type=3Dradio = id=3D"ship4"=20 > name=3D"ship_option" value=3D"4" Checked></TD> > <TD width=3D65 class=3D"boxfields" align=3D"center" = valign=3D"top"> > <img src=3D"images/shipping/ups_2D.gif" alt=3D"LEM = Domestic=20 > "><br><b>$10.00</b></TD> > </label> >=20 > </tr> > </table> > </td> > </tr>=20 >=20 > <tr>=20 >=20 > <td><IMG=20 > SRC=3D"https://www.host.com/htmltemp_consol/images/spacer.gif" = WIDTH=3D10=20 > HEIGHT=3D5></TD> > </tr>=20 >=20 > </table>=20 >=20 > <!--end of pieces=20 > table--> > </td>=20 >=20 > </tr>=20 >=20 > </table>=20 >=20 > <!--end of second color=20 > table--> > </TD>=20 >=20 > </TR>=20 >=20 > </table>=20 >=20 > <!--end of Shipping type Section--> >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real = users. > Discover which products truly live up to the hype. Start reading now.=20 > http://productguide.itmanagersjournal.com/ > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users > |