BlenderHead has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know of a tutorial/example of how to make a table with rows and columns using loops in HTML::Template?

I can already do rows in HTML::Template with relative ease, but how to do rows AND columns elude me.

Suppose for example I did a DB call and had a hash with eighteen different references, and I wanted to put it into a table that was four columns wide. Each column would get a reference, and - with eighteen references - it would require five rows with two columns left over.

What I am doing is trying to figure out how to generate the loop to format the table, so that it stops after four columns, and then starts a new row.

Any ideas? Examples?

TIA, Everyone.

:)

  • Comment on How to create a table with rows and columns using HTML::Template loops?

Replies are listed 'Best First'.
Re: How to create a table with rows and columns using HTML::Template loops?
by wfsp (Abbot) on Jun 24, 2009 at 07:35 UTC
    One way. You'll need to adapt it to suit your input.
    #! /usr/bin/perl use strict; use warnings; use HTML::Template; use Data::Dumper; $Data::Dumper::Indent = 2; my %record = ( cell_01 => 1, cell_02 => 2, cell_03 => 3, cell_04 => 4, cell_05 => 5, cell_06 => 6, cell_07 => 7, cell_08 => 8, cell_09 => 9, cell_10 => 10, cell_11 => 11, cell_12 => 12, cell_13 => 13, cell_14 => 14, cell_15 => 15, cell_16 => 16, cell_17 => 17, cell_18 => 18, ); my @cells = map{ {cell => $record{$_}} } sort keys %record; my (@row, @rows); my $i = 0; while ($i < $#cells){ for my $col (0..3){ last if $i > $#cells; push @row, $cells[$i++]; } push @rows, {td_loop => [@row]}; @row = (); } #print Dumper \@rows; my $t = HTML::Template->new(filehandle => *DATA) or die qq{cant load templ}; $t->param(tr_loop => [@rows]); print $t->output; __DATA__ <table> <TMPL_LOOP tr_loop> <tr> <TMPL_LOOP td_loop> <td><TMPL_VAR cell></td> </TMPL_LOOP> </tr> </TMPL_LOOP> </table>
    output (whitespace trimmed)
    <table> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> <tr> <td>9</td> <td>10</td> <td>11</td> <td>12</td> </tr> <tr> <td>13</td> <td>14</td> <td>15</td> <td>16</td> </tr> <tr> <td>17</td> <td>18</td> </tr> </table>
Re: How to create a table with rows and columns using HTML::Template loops?
by porta (Sexton) on Jun 24, 2009 at 03:30 UTC
    ##UPDATE I just re read your message and noticed this wont work for you. Sorry. This have been doing the trick for me. For the html template:
    <table> <thead> <tr> <!-- tmpl_loop name="fields" --> <th><!-- tmpl_var name="field" --></th> <!-- /tmpl_loop --> </tr> </thead> <tbody> <!-- tmpl_loop name="data" --> <tr> <!-- tmpl_loop name="cols" --> <td><!-- tmpl_var name="value" --></td> <!-- /tmpl_loop --> </tr> <!-- /tmpl_loop --> </tbody> </table>
    And, the perl part:
    my @fields = qw(your columns names here); $object->get_all(%params); while (my $obj = $object->get_next){ my @row = map {{value => $obj->$_}} @fields; push @data, {cols => \@row}; } $template->param(fields => \@fields); $template->param(data => \@data);
    Hope this helps.

      If then anyone can help, the question still holds!

      Thanks for trying!

      BH