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

I want to use "TMPL_IF NAME=TC_Rst_OK" in a "TMPL_LOOP ", (I have set "loop_context_vars=>1"). My question is can I change the "$template->param(TC_Rst_OK)" dynamically. Like:
if(True){ $template->param(TC_Rst_OK=>0); }else{ $template->param(TC_Rst_OK=>0); }
Sample.tmpl:
<TMPL_LOOP NAME=TC_Loop> <tr> <td><TMPL_VAR NAME=TC_Num></td> <td><TMPL_VAR NAME=TC_Name></td> <TMPL_IF NAME=TC_Rst_OK> <td style="color:Blue"><TMPL_VAR NAME=TC_Rst></td> <TMPL_ELSE> <td style="color:Red"><TMPL_VAR NAME=TC_Rst></td> </TMPL_IF> <td><TMPL_VAR NAME=TC_Time></td> <td>Expand</td> </tr> </TMPL_LOOP>

Replies are listed 'Best First'.
Re: HTML::Template how to use IF/Else in a LOOP
by moritz (Cardinal) on Jan 20, 2011 at 20:50 UTC
    By default, only variables from the loop itself are accessible inside the loop. So if you have
    $tmpl->param(a => [ { b => 1}, { b => 2 }]);

    and a <TMPL_LOOP VAR=a>, then only the variable b is available within the loop, and no other variables, independently of other param() calls.

    You can change that by setting global_vars => 1 in the constructor.

      and since one might avoid globals vars also in templates (I do): you might try HTML::Template::Compiled, where you can activate global vars partly (see "global_vars" in the OPTIONS section) - it allows you to "navigate" up the data structure, so <TMPL_VAR ..foo> will get you the var foo directly one level above (and 3 dots will go two levels up and so on (notice also that one dot goes to the root level, and for that you don't even need to set global_vars)).
Re: HTML::Template how to use IF/Else in a LOOP
by calabash (Novice) on Jan 26, 2011 at 07:58 UTC
    Thanks for the advices! Finally I solved my former request by adding another <TMPL_VAR> to describe color in the loop. So the color will be changed automatically for each case.
    <TMPL_LOOP NAME=TC_Loop> <tr> <td><TMPL_VAR NAME=TC_Num></td> <td style="color:DarkSlateGray "><TMPL_VAR NAME=TC_Name></td> <td style=<TMPL_VAR NAME=TC_Rst_Color>><TMPL_VAR NAME=TC_Rst></td> <td style=<TMPL_VAR NAME=TC_Rst_Color>><TMPL_VAR NAME=TC_Time></td +> <td ><a href=<TMPL_VAR NAME=TC_Ref>>ExpandRef</a></td> </tr> </TMPL_LOOP>