Thread: [htmltmpl] TMPL_IF and TMPL_ELSE - how do I use this?
Brought to you by:
samtregar
From: LDT <per...@ya...> - 2004-02-17 19:37:15
|
I've read the documentation sections on TMPL_IF and TMPL_ELSE. The part where I'm getting stuck is figuring out how to set up that "BOOL" value. I know if "BOOL" is true, then the section in TMPL_IF is run; if it's false, the section in TMPL_ELSE is run. This sounds like what I want to do (I think...). I have my results generated and the web page looks nice. However, there are some rows that need to have a different bit of HTML in it (some rows/cells are to be "drillable" and some are not). Unfortunately, it's not just the "last" or "first", etc.; the rows are interspersed in no particular order, some are contiguous, some are stand-alone. This is where I get stuck. I can tell where I am in Perl when I'm pushing the hash references to the @loop_data (which is subsequently pushed to the template for output), so I can set a variable to something at that time. How do I set up a variable that the TMPL_IF and TMPL_ELSE can use to determine which section of the HTML loop to process? I hope this made sense. When you're new, sometimes it's hard to know even how to ask a question! :-) Thanks! Lori --------------------------------- Do you Yahoo!? Yahoo! Finance: Get your refund fast by filing online |
From: P K. <pk...@ei...> - 2004-02-17 19:51:14
|
LDT wrote: > I've read the documentation sections on TMPL_IF and TMPL_ELSE. The > part where I'm getting stuck is figuring out how to set up that "BOOL" > value. I know if "BOOL" is true, then the section in TMPL_IF is run; > if it's false, the section in TMPL_ELSE is run. This sounds like what > I want to do (I think...). > > I have my results generated and the web page looks nice. However, > there are some rows that need to have a different bit of HTML in it > (some rows/cells are to be "drillable" and some are not). > Unfortunately, it's not just the "last" or "first", etc.; the rows are > interspersed in no particular order, some are contiguous, some are > stand-alone. you will have to set up some variable that is present in every row, and then set that variable to 1 or 0... @rows = ( {drillable=>0......}, {drillable=>0......}, {drillable=>1......} ); then, in your loop <tmpl_loop rows> <tmpl_if drillable>print drillable cells<tmpl_else>print cells</tmpl_if> </tmpl_loop> |
From: Timm M. <tm...@ag...> - 2004-02-17 19:54:41
|
You set it up just like a TMPL_VAR. If the value of that variable is true (according to Perl's definition of truth), then that section is run. It's just like doing: if($var) { # Do something } else { # Do something else } Except HTML::Template (in its basic form) doesn't provide a way of doing elsif or doing complex comparisons (like $var == 3). At 11:32 AM 2/17/04 -0800, LDT wrote: >I've read the documentation sections on TMPL_IF and TMPL_ELSE. The part >where I'm getting stuck is figuring out how to set up that "BOOL" >value. I know if "BOOL" is true, then the section in TMPL_IF is run; if >it's false, the section in TMPL_ELSE is run. This sounds like what I want >to do (I think...). > >I have my results generated and the web page looks nice. However, there >are some rows that need to have a different bit of HTML in it (some >rows/cells are to be "drillable" and some are not). Unfortunately, it's >not just the "last" or "first", etc.; the rows are interspersed in no >particular order, some are contiguous, some are stand-alone. > >This is where I get stuck. I can tell where I am in Perl when I'm pushing >the hash references to the @loop_data (which is subsequently pushed to the >template for output), so I can set a variable to something at that time. > >How do I set up a variable that the TMPL_IF and TMPL_ELSE can use to >determine which section of the HTML loop to process? > >I hope this made sense. When you're new, sometimes it's hard to know even >how to ask a question! :-) > >Thanks! > >Lori > > >Do you Yahoo!? >Yahoo! Finance: ><http://us.rd.yahoo.com/evt=22055/*http://taxes.yahoo.com/filing.html>Get >your refund fast by filing online |
From: Roger B. W. <ro...@fi...> - 2004-02-17 20:00:12
|
On Tue, Feb 17, 2004 at 11:32:43AM -0800, LDT wrote: >How do I set up a variable that the TMPL_IF and TMPL_ELSE can use to determine which section of the HTML loop to process? Set a new variable in the hash that forms the entry in @loop_data. Set it to "1" for the special mode, and don't set it at all (or set it to "" or 0) for the normal mode. If you post code fragments I'll show you how this can be done. Without seeing your specific coding style, I can't do any better than the various examples that are in the list archive. Roger |
From: Joel <htm...@jo...> - 2004-02-17 20:04:21
|
Hi Lori, In your loop where you are pushing onto @loop_data, you'd want to add an additional variable to your hashref that will indicate whether or not is drillable. So inside your loop, you might do something like this: for (some_loop_condition) { if (row_is_drillable) { push @loop_data, {id =3D> $this, rowname =3D> $that, drillable =3D> 1} } else { push @loop_data, {id =3D> $this, rowname =3D> $that, drillable =3D> 0} =20 } } $tmpl->param(LOOP_DATA =3D> \@loop_data); Then inside your template: <TMPL_LOOP NAME=3DLOOP_DATA> ID: <TMPL_VAR NAME=3DID><br> Name: <TMPL_VAR NAME=3DROWNAME><br> Drillable?: <TMPL_IF NAME=3DDRILLABLE>Yes<TMPL_ELSE>No</TMPL_IF> </TMPL_LOOP> Basically, you can use TMPL_IF on any variable in your loop. Although, if a drillable row requires a URL or some other bit of information, you can get = away without an explicit drillable variable and use the presence or absence of t= he URL to indicate if the row is drillable. Remember what true and false are in perl and that will help you. False is 0 (the numeric value), "0" (a string containing the number 0), undef (undefin= ed) or a empty string ('' or ""). Anything else is true. And that's how HTML::Tempalte works, as well, when dealing with TMPL_IF. Clear as mud, right? :) Hope this helps. Good luck! --Joel >I've read the documentation sections on TMPL_IF and TMPL_ELSE. The part >where I'm getting stuck is figuring out how to set up that "BOOL" value. = I >know if "BOOL" is true, then the section in TMPL_IF is run; if it's false, >the section in TMPL_ELSE is run. This sounds like what I want to do (I >think...). >=20 >I have my results generated and the web page looks nice. However, there a= re >some rows that need to have a different bit of HTML in it (some rows/cells >are to be "drillable" and some are not). Unfortunately, it's not just the >"last" or "first", etc.; the rows are interspersed in no particular order, >some are contiguous, some are stand-alone. >=20 >This is where I get stuck. I can tell where I am in Perl when I'm pushing >the hash references to the @loop_data (which is subsequently pushed to the >template for output), so I can set a variable to something at that time. = =20 >=20 >How do I set up a variable that the TMPL_IF and TMPL_ELSE can use to >determine which section of the HTML loop to process? >=20 >I hope this made sense. When you're new, sometimes it's hard to know even >how to ask a question! :-) >=20 >Thanks! >=20 >Lori > > >--------------------------------- >Do you Yahoo!? >Yahoo! Finance: Get your refund fast by filing online |