Time to break the problem down! First off, have you read my other tutorial Map: The Basics? Anyhoo, any time i deal with map and complex data structures, i always bring Data::Dumper along for the ride, consider this piece of code first:
use strict; use HTML::Template; use Data::Dumper; my @array = ( [(1..5)], [qw(one two three four five)], [qw(ein zwei drei veir funf)], [qw(hana dool set net dasut)], [qw(yi er san si wu)], ); print Dumper \@array;
This will give you an idea of what your data structure looks like. In this case, it is a two dimensional array, or an array of array references. Moving ahead, let's now build a template to work with:
<table> <tmpl_loop rows> <tr> <tmpl_loop cells> <td><tmpl_var data></td> </tmpl_loop> </tr> </tmpl_loop> </table>
So, in order for this to work, we need a need to pass the the param() method one arg named 'rows', and that "key" must point to a list of hash references. Each of these hash references will have a one key, 'cells'. For now, let's just focus on the first list of hash references. We will use map to transform the list of array references from @array into a list of hash refs. Each key will be called 'cells' and the values will be each of the inner array references from @array. Read from right to left:
my $rows = [ map {{ cells => $_ }} @array ]; print Dumper $rows;
The whole map statement must be 'wrapped' by brackets to specify that $rows is really an array reference.

Now all we need to do is turn each of those elements into yet another hash ref, yet another single key, 'data':

my $rows = [ map {{ cells => [ map {{ data => $_ }} @$_ ] }} @array ]; print Dumper $rows;
That is what HTML::Template wants - each loop must be a list of hash references. And yes, this is not easy stuff! :) (This is also why i wrote DBIx::XHTML_Table, but that is another story.) Here a complete script that you can play with. Good luck! :)
use strict; use HTML::Template; use Data::Dumper; my @array = ( [(1..5)], [qw(one two three four five)], [qw(ein zwei drei veir funf)], [qw(hana dool set net dasut)], [qw(yi er san si wu)], ); my $rows = [ map {{ cells => [ map {{ data => $_ }} @$_ ] }} @array ]; my $data = do {local $/;<DATA>}; my $tmpl = HTML::Template->new( scalarref => \$data, ); $tmpl->param( rows => $rows ); print $tmpl->output; __DATA__ <table> <tmpl_loop rows> <tr> <tmpl_loop cells> <td><tmpl_var data></td> </tmpl_loop> </tr> </tmpl_loop> </table>

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) Re: HTML table with HTML::Template and loop in a loop by jeffa
in thread HTML table with HTML::Template and loop in a loop by fireartist

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.