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. :-)
In reply to Re: Nested loops in HTML::Template
by ChemBoy
in thread Nested loops in HTML::Template
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |