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

I asked around and crazyinsomniac suggested a sample data and layout example to help explain what I am asking for, so here it is:
data.dat
--------
id|picture|name
1|seans.gif|Sean Shrum
2|steves.gif|Steve Shrum
3|dianas.gif|Diana Shrum
4|marthas.gif|Martha Shrum

Laying out the about data in a straight 1 x format is simple, I can do this myself without the aid of a templating module like:

1 - PICTURE - Sean Shrum
2 - PICTURE - Steve Shrum
3 - PICTURE - Diana Shrum
4 - PICTURE - Martha Shrum

...but what I want to be able to do is place X records in a row. The layout would be defined in a record template but ultimately would allow me to produce something like (if I wanted 3 to a row):

   PICTURE           PICTURE            PICTURE
1 - Sean Shrum   2 - Steve Shrum    3 - Diana Shrum

    PICTURE
4 - Martha Shrum

I'm half-way tempted to just code the thing myself. I already do page nesting via s// using tokens and field replacements via field tokens. This is simply a issue of knowing when to insert the /TR tag. But I regress...is there a module that would allow me to do something like this. I have looked over the HTML::Template documentation and from the looks of it, it cannot do this sort of layout.

If I am incorrect, please point me to a sample template and code snippet that demonstrates this implementation. Granted, HTML::Template would be great since it was a easy install. However, if I am correct, can someone point me to a module that acheives this. I have no issues with dropping HTML::Template like an IPO from a new internet company (in todays market) to switch to something else.

TIA - All input is welcome.

======================
Sean Shrum
http://www.shrum.net

  • Comment on Grouping multiple records in a row...templating ?

Replies are listed 'Best First'.
Re: Grouping multiple records in a row...templating ?
by Kanji (Parson) on Apr 18, 2002 at 11:03 UTC

    I haven't used HTML::Template (alt.), but it looks like the HTML::Template::Expr (alt.) extension provides the functionality you want...

    <table><tr> <TMPL_LOOP NAME="PICTURES"> <td><TMPL_VAR NAME="NAME"></td> <TMPL_UNLESS EXPR="ID % 3"> </tr><tr> </TMPL_UNLESS> </TMPL_LOOP> </tr></table>

    Update: Simplified (and tested ;)) the template.

        --k.


      Without using HTML::Template::Expr or making the assumption that id's are sequential (which has bitten me before) you can do something like this in your Perl code:
      $_->{nextrow) = ($i++ % 3) for @pictures;

      Hope this helps.

      gav^

Re: Grouping multiple records in a row...templating ?
by lachoy (Parson) on Apr 18, 2002 at 12:21 UTC

    The Template Toolkit can do this as well using one of the built-in plugins:

    #!/usr/bin/perl use strict; use Template; my @raw_data = ( '1|seans.gif|Sean Shrum', '2|steves.gif|Steve Shrum', '3|dianas.gif|Diana Shrum', '4|marthas.gif|Martha Shrum' ); my @data = map { [ split '\|', $_ ] } @raw_data; my $template = Template->new(); $template->process( \*DATA, { data => \@data, column_num => 3 } ) || die $template->error(); __DATA__ [%- USE table( data, rows = column_num ) -%] [% FOREACH col = table.cols -%] <tr> [%- FOREACH item = col -%] [%- IF item %] <td><img src="[% item.1 %]"><br> [% item.0 %] [% item.2 %]</td> [%- ELSE %] <td>&nbsp;</td> [%- END %] [%- END %] </tr> [% END -%]

    Which produces:

    <tr> <td><img src="seans.gif"><br> 1 Sean Shrum</td> <td><img src="steves.gif"><br> 2 Steve Shrum</td> <td><img src="dianas.gif"><br> 3 Diana Shrum</td> </tr> <tr> <td><img src="marthas.gif"><br> 4 Martha Shrum</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr>

    The table plugin takes a little getting used to, but it's extremely handy.

    Chris
    M-x auto-bs-mode

Re: Grouping multiple records in a row...templating ?
by kappa (Chaplain) on Apr 18, 2002 at 11:06 UTC
    And how about this (the simplest and brute-force solution):

    Template

    <table> <tmpl_loop name=TABLE> <tr> <tmpl_loop name=row> <td><tmpl_var name=name> : <tmpl_var name=picture></td +> </tmpl_loop> </tr> </tmpl_loop> </table>

    ... and code:
    use HTML::Template; my $table = [ { row => [ { name => a11, picture => aaa }, { name => a12, picture => aaa }, { name => a13, picture => aaa }, ] }, { row => [ { name => a21, picture => aaa }, { name => a22, picture => aaa }, { name => a23, picture => aaa }, ] }, { row => [ { name => a31, picture => aaa }, { name => a32, picture => aaa }, { name => a33, picture => aaa }, ] }, ]; my $t = HTML::Template->new(filename => 'aa.tmpl'); $t->param(TABLE => $table); print $t->output;

    And you'd better pad the last incomplete row with some dummy elements.