|
I just started using TT and had a question about iterating over an array.
I want to put a fixed number of array items on a HTML row. Something like the
following:
# Have an array of hash: my @array = map
{{id=>"abc$_"}} (1..99); my $aref = \@array; my $vars = {
aref => $aref };
# Want to mimic this in template: for
(0..$#{@$aref}) { print "<tr>" if $_ == 0; print
"<td>$aref->[$_]{id}</td>"; print
"</tr>\n<tr>" unless ($_+1)%4; print "</tr>" if $_
== $#{@$aref}; }
# To get this
output: <tr><td>abc1</td><td>abc2</td><td>abc3</td><td>abc4</td></tr> <tr><td>abc5</td><td>abc6</td><td>abc7</td><td>abc8</td></tr> <tr><td>abc9</td><td>abc10</td><td>abc11</td><td>abc12</td></tr> <tr><td>abc13</td><td>abc14</td><td>abc15</td><td>abc16</td></tr>
I
know how to do a simple iteration using [% FOREACH aref %] and I know about
aref.size which would give the equivalent of scalar(@$aref) but I'm not sure
how to accomplish the above. Any help would be great.
The
"Table" plugin suits well for this case. You should use "inverted" table (rows
becomes cols and vice versa) to get desired output. So rows=3 here really means
number of columns.
[% USE
table(aref, rows=3) %]
<table> [% FOREACH cols = table.cols %]
<tr> [% FOREACH item =
cols %] <td>[% item
%]</td> [% END %]
</tr> [% END %]
</table>
-- Sergey Martynoff
|