It seems vladb has me beaten to the punch somewhat, but I'll give my solution as well, if only because it does, in fact, use your original template (right down to the typos ;-).

#!/usr/bin/perl use HTML::Template; my $template = HTML::Template->new(filehandle => *DATA); $template->param(title=>"Foo"); @filenames = ([qw(foo bar baz)], [qw(Ein Zwei Drei)], [qw(beer pizza caffeine)], [qw(Vader Sidious Maul Tyrranos)] ); $template->param(dir_loop => [{dir => "Dir1", outter_file_loop => [ {inner_file_loop => [map {filename=> +$_}, @{$filenames[0]}]}, {inner_file_loop => [map {filename=> +$_}, @{$filenames[1]}]}, ] }, {dir => "Dir2", outter_file_loop => [ {inner_file_loop => [map {filename=> +$_}, @{$filenames[2]} ] }, {inner_file_loop => [map {filename=> +$_}, @{$filenames[3]} ] }, ] }, ]); print $template->output; __DATA__ # original template here

Possibly worth pointing out: you don't need to use especially long names for your template vars and loops--unless you go out of your way to break it (which you shouldn't), HTML::Template kindly scopes your variables for you, so you could (which you also shouldn't) simply call all of your loops "loop", as long as you didn't put multiple loops at any level. Obviously, a comfortable middle ground can and should be found. :-)

Possibly worth suggesting to the author, if he ever reads this site--wouldn't it be nice if there was a way to do something like map in the template, to avoid all those hashrefs? Perhaps along these lines?

$template->param(files=>[qw (/etc/passwd /var/log/httpd/access_log) ]) +; __END__ <TMPL_MAP name=files> <TD><TMPL_MAPVAR></TD> </TMPL_MAP>

Of course, submitting that to the author with a patch would be the way to go, wouldn't it... perhaps some other day. :-)



If God had meant us to fly, he would *never* have given us the railroads.
    --Michael Flanders


In reply to Re: Nested loops in HTML::Template by ChemBoy
in thread Nested loops in HTML::Template by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.