On Mon, Jun 24, 2002 at 08:08:50AM -0700, Roy Rubin wrote:
>Can someone provide an example of nested loops. I have it working, but I am
>using *bad* code because I have had problems with referencing.

Code roughly like this:

my @l1;
foreach my $outer (@outer_loop_list) {
  my %r1=(outer => $outer);
  # add more outer-loop stuff here in %r1
  my @l2;
  foreach my $inner (@inner_loop_list) {
    my %r2=(inner => $inner);
    # add more inner-loop stuff here in %r2
    push @l2,\%r2;
  }
  $r1{innerloop}=\@l2;
  push @l1,\%r1;
}
$tmpl->param(outerloop => \@l1);

For a template like this:

<table>
<tmpl_loop name=outerloop>
<tr><td><tmpl_var name=outer escape=html></td>
<tmpl_loop name=innerloop>
<td><tmpl_var name=inner escape=html></td>
</tmpl_loop>
</tr>
</tmpl_loop>
</table>


Once you get the hang of it, you'll find that you can often omit some of
the intermediate variables; this is the fully-laid-out version so that
you can see what's going on.

(Note that I haven't checked this particular code fragment, but it
usually works first time when I do this for real...)

Roger

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to