[cgiapp] Variable Number of Column in a Table

Stephen Carville stephen.carville at gmail.com
Sun May 18 19:54:34 EDT 2008


On 5/15/08, Cees Hek <ceeshek at gmail.com> wrote:
> On Fri, May 16, 2008 at 2:03 AM, Stephen Carville
> <stephen.carville at gmail.com> wrote:
>> Is there any way to have a template where the number of columns is
>>  determined at run time?  I know how to use TMPL_LOOP but that only
>>  lets me vary the number of rows.
>
> I realize that you are using HTML::Template, and you may be tied to it
> for reasons good or bad, but
> whenever I see people trying to bend a tool to do things against it's
> will I feel obliged to show another way ;)

I'm not tried to HTML::Template.  I'm experimenting with it.  The goal
is to a create a generic module that will let a developer create a
script with a useful name like "XML.Orders.By.Customer.cgi" where
he'll select the inputs and prompts, drop in the sql for the
search(es), select a table format have the table(s) output as a web
page.  He could also write custom post processors for the data JIC it
need to be massaged for presentation.

After that I may add some bells and whistles such drill down or
letting the user selecting which columm to search on

I already use CGI::Application and HTML::Template to pull data from
the central syslog server's mysql database where it performs pretty
well but that only three columnn and matbe 6000 rows.

> In my example below, the data is provided in a format that is easy to
> generate using a DBI call, and the template itself is pretty straight
> forward.  Row colouring is also made easy using the Cycle plugin (if
> you want to alternate between three colours, just add another entry
> into the cycle and leave the rest of the template as is).
>
> use Template;
>
> my $template = Template->new( POST_CHOMP => 1 );
> my $data = {
>     headers => [qw(col1 col2 col3 col4)],
>     rows    => [
>         [ qw(1.1 1.2 1.3 1.4) ],
>         [ qw(2.1 2.2 2.3 2.4) ],
>         [ qw(3.1 3.2 3.3 3.4) ],
>         [ qw(4.1 4.2 4.3 4.4) ],
>         [ qw(5.1 5.2 5.3 5.4) ],
>         [ qw(6.1 6.2 6.3 6.4) ],
>     ],
> };
> $template->process(\*DATA, $data) or die $template->error;
> __END__
> [% USE rowclass = Cycle('oddclass', 'evenclass') %]
> <table>
>  <tr>
> [% FOREACH col IN headers %]
>   <th>[% col %]</th>
> [% END %]
>  </tr>
> [% FOREACH row IN rows %]
>  <tr class="[% rowclass %]">
> [%   FOREACH col IN row %]
>    <td>[% col %]</td>
> [%   END %]
>  </tr>
> [% END %]
> </table>

Hmmm.  The curent template (which I wrote about 5 years ago) does
somehting similar.  It returns the data in a hash where the key is the
sort column.  Maybe your approach is better adapted to that.

FWIW, I also found that alternating colors works "better" if I use css
to define the row classes

tr.d0 td { background-color: #FFFFFF; color: black; }
tr.d1 td { background-color: #C0C0C0; color: black; }

and in the loop

<TMPL_LOOP NAME=cols>
  <tr class="<TMPL_VAR NAME=RowClass>">
    <td><TMPL_VAR NAME=ReceivedAt>
    <td><TMPL_VAR NAME=Message>
    <td><TMPL_VAR NAME=SyslogTag>
  </tr>
</TMPL_LOOP>

Then set RowClass = ( $row % 2 ? "d0" : "d1" );

-- 
Stephen Carville


More information about the cgiapp mailing list