Variable Number of Column in a Table

View: New views
11 Messages — Rating Filter:   Alert me  

Variable Number of Column in a Table

by Stephen Carville-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

What I'm doing is creating a template for SQL reports so I want a
structure like:

  <TMPL_LOOP NAME=SQL_OUTPUT>
    <TMPL_IF NAME="__first__">
      Headers go here
    </TMPL_IF>

    <TMPL_IF NAME="__odd__">
      Row of data in odd row color set
    </TMPL_IF>

    <TMPL_UNLESS NAME="__odd__">
      Row of data in even row color set
    </TMPL_UNLESS>
 </TMPL_LOOP>


--
Stephen Carville

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


Re: Variable Number of Column in a Table

by Joshua Miller-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, but it's not pretty...

 <TMPL_LOOP NAME=SQL_OUTPUT>
   <TMPL_IF NAME="__first__">
     <TR>
     <TMPL_LOOP NAME=COLS>
       <TH>Headers go here</TH>
     </TMPL_LOOP>
     </TR>
   </TMPL_IF>

   <TMPL_IF NAME="__odd__">
     <TR>
     <TMPL_LOOP NAME=COLS>
       <TD>Row of data in odd row color set</TD>
     </TMPL_LOOP>
     </TR>
   </TMPL_IF>

   <TMPL_UNLESS NAME="__odd__">
     <TR>
     <TMPL_LOOP NAME=COLS>
       <TD>Row of data in even row color set</TD>
     </TMPL_LOOP>
     </TR>
   </TMPL_UNLESS>
 </TMPL_LOOP>

$data = {
    SQL_OUTPUT => [
        { COLS => [ { CELLDATA => "blah" },
                    { CELLDATA => "blah" },
                    { CELLDATA => "blah" },
                  ] },
        { COLS => [ { CELLDATA => "blah" },
                    { CELLDATA => "blah" },
                    { CELLDATA => "blah" },
                  ] },
        ],
    };

--
Josh I.

On Thu, May 15, 2008 at 12:03 PM, Stephen Carville <
stephen.carville@...> 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.
>
> What I'm doing is creating a template for SQL reports so I want a
> structure like:
>
>  <TMPL_LOOP NAME=SQL_OUTPUT>
>    <TMPL_IF NAME="__first__">
>      Headers go here
>    </TMPL_IF>
>
>    <TMPL_IF NAME="__odd__">
>      Row of data in odd row color set
>    </TMPL_IF>
>
>    <TMPL_UNLESS NAME="__odd__">
>      Row of data in even row color set
>    </TMPL_UNLESS>
>  </TMPL_LOOP>
>
>
> --
> Stephen Carville
>
> #####  CGI::Application community mailing list  ################
> ##                                                            ##
> ##  To unsubscribe, or change your message delivery options,  ##
> ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
> ##                                                            ##
> ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
> ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
> ##                                                            ##
> ################################################################
>
>

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


Re: Variable Number of Column in a Table

by Sean Davis-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, May 15, 2008 at 12:21 PM, Joshua Miller <unrtst@...> wrote:
> Yes, but it's not pretty...

And it may be pretty slow for largish result sets.

You could also consider using client-side javascript to do the table
layout and sending the data as JSON, but that obviously adds a level
of complexity.


>  <TMPL_LOOP NAME=SQL_OUTPUT>
>   <TMPL_IF NAME="__first__">
>     <TR>
>     <TMPL_LOOP NAME=COLS>
>       <TH>Headers go here</TH>
>     </TMPL_LOOP>
>     </TR>
>   </TMPL_IF>
>
>   <TMPL_IF NAME="__odd__">
>     <TR>
>     <TMPL_LOOP NAME=COLS>
>       <TD>Row of data in odd row color set</TD>
>     </TMPL_LOOP>
>     </TR>
>   </TMPL_IF>
>
>   <TMPL_UNLESS NAME="__odd__">
>     <TR>
>     <TMPL_LOOP NAME=COLS>
>       <TD>Row of data in even row color set</TD>
>     </TMPL_LOOP>
>     </TR>
>   </TMPL_UNLESS>
>  </TMPL_LOOP>
>
> $data = {
>    SQL_OUTPUT => [
>        { COLS => [ { CELLDATA => "blah" },
>                    { CELLDATA => "blah" },
>                    { CELLDATA => "blah" },
>                  ] },
>        { COLS => [ { CELLDATA => "blah" },
>                    { CELLDATA => "blah" },
>                    { CELLDATA => "blah" },
>                  ] },
>        ],
>    };
>
> --
> Josh I.
>
> On Thu, May 15, 2008 at 12:03 PM, Stephen Carville <
> stephen.carville@...> 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.
>>
>> What I'm doing is creating a template for SQL reports so I want a
>> structure like:
>>
>>  <TMPL_LOOP NAME=SQL_OUTPUT>
>>    <TMPL_IF NAME="__first__">
>>      Headers go here
>>    </TMPL_IF>
>>
>>    <TMPL_IF NAME="__odd__">
>>      Row of data in odd row color set
>>    </TMPL_IF>
>>
>>    <TMPL_UNLESS NAME="__odd__">
>>      Row of data in even row color set
>>    </TMPL_UNLESS>
>>  </TMPL_LOOP>
>>
>>
>> --
>> Stephen Carville
>>
>> #####  CGI::Application community mailing list  ################
>> ##                                                            ##
>> ##  To unsubscribe, or change your message delivery options,  ##
>> ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
>> ##                                                            ##
>> ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
>> ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
>> ##                                                            ##
>> ################################################################
>>
>>
>
> #####  CGI::Application community mailing list  ################
> ##                                                            ##
> ##  To unsubscribe, or change your message delivery options,  ##
> ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
> ##                                                            ##
> ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
> ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
> ##                                                            ##
> ################################################################
>
>

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


Re: Variable Number of Column in a Table

by Joshua Miller-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I was curious if "may be" was true, and if "pretty slow" was actually
significant, so I did a quick benchmark (and yes, it is  much slower, but
depending on your app, that may not matter).

I've attached the test script, but incase it doesn't come through...
First test, pure perl loop over array of arrays, 100 rows, 500 columns per
row, building an HTML table.
Second test, HTML::Template, TMPL_LOOP over the rows, with 500 statically
defined columns as TMPL_VAR's (again, 100 rows, 500 columns).
Third test, HTML::Template, TMPL_LOOP over the rows, and an inner TMPL_LOOP
over the columns - 100 rows, 500 columns.

40 iterations, Benchmark.pm reports:
Benchmark: timing 40 iterations of perl, tmpl1loop, tmpl2loops...
      perl:  0 wallclock secs ( 0.68 usr +  0.01 sys =  0.69 CPU) @ 57.97/s
(n=40)
            (warning: too few iterations for a reliable count)
 tmpl1loop: 12 wallclock secs (11.72 usr +  0.06 sys = 11.78 CPU) @  3.40/s
(n=40)
tmpl2loops: 40 wallclock secs (39.34 usr +  0.08 sys = 39.42 CPU) @  1.01/s
(n=40)


So, with two loops, it was about 1second per iteration. If you really need
the speed though, removing the templates (and the more complex data
structure) sped things up considerably.

Let me know if the attachments don't come through and you want a copy.
--
Josh I.




On Thu, May 15, 2008 at 12:29 PM, Sean Davis <sdavis2@...> wrote:

> On Thu, May 15, 2008 at 12:21 PM, Joshua Miller <unrtst@...> wrote:
> > Yes, but it's not pretty...
>
> And it may be pretty slow for largish result sets.
>
> You could also consider using client-side javascript to do the table
> layout and sending the data as JSON, but that obviously adds a level
> of complexity.
>
>
> >  <TMPL_LOOP NAME=SQL_OUTPUT>
> >   <TMPL_IF NAME="__first__">
> >     <TR>
> >     <TMPL_LOOP NAME=COLS>
> >       <TH>Headers go here</TH>
> >     </TMPL_LOOP>
> >     </TR>
> >   </TMPL_IF>
> >
> >   <TMPL_IF NAME="__odd__">
> >     <TR>
> >     <TMPL_LOOP NAME=COLS>
> >       <TD>Row of data in odd row color set</TD>
> >     </TMPL_LOOP>
> >     </TR>
> >   </TMPL_IF>
> >
> >   <TMPL_UNLESS NAME="__odd__">
> >     <TR>
> >     <TMPL_LOOP NAME=COLS>
> >       <TD>Row of data in even row color set</TD>
> >     </TMPL_LOOP>
> >     </TR>
> >   </TMPL_UNLESS>
> >  </TMPL_LOOP>
> >
> > $data = {
> >    SQL_OUTPUT => [
> >        { COLS => [ { CELLDATA => "blah" },
> >                    { CELLDATA => "blah" },
> >                    { CELLDATA => "blah" },
> >                  ] },
> >        { COLS => [ { CELLDATA => "blah" },
> >                    { CELLDATA => "blah" },
> >                    { CELLDATA => "blah" },
> >                  ] },
> >        ],
> >    };
> >
> > --
> > Josh I.
> >
> > On Thu, May 15, 2008 at 12:03 PM, Stephen Carville <
> > stephen.carville@...> 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.
> >>
> >> What I'm doing is creating a template for SQL reports so I want a
> >> structure like:
> >>
> >>  <TMPL_LOOP NAME=SQL_OUTPUT>
> >>    <TMPL_IF NAME="__first__">
> >>      Headers go here
> >>    </TMPL_IF>
> >>
> >>    <TMPL_IF NAME="__odd__">
> >>      Row of data in odd row color set
> >>    </TMPL_IF>
> >>
> >>    <TMPL_UNLESS NAME="__odd__">
> >>      Row of data in even row color set
> >>    </TMPL_UNLESS>
> >>  </TMPL_LOOP>
> >>
> >>
> >> --
> >> Stephen Carville
> >>
> >> #####  CGI::Application community mailing list  ################
> >> ##                                                            ##
> >> ##  To unsubscribe, or change your message delivery options,  ##
> >> ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
> >> ##                                                            ##
> >> ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
> >> ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
> >> ##                                                            ##
> >> ################################################################
> >>
> >>
> >
> > #####  CGI::Application community mailing list  ################
> > ##                                                            ##
> > ##  To unsubscribe, or change your message delivery options,  ##
> > ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
> > ##                                                            ##
> > ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
> > ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
> > ##                                                            ##
> > ################################################################
> >
> >
>
> #####  CGI::Application community mailing list  ################
> ##                                                            ##
> ##  To unsubscribe, or change your message delivery options,  ##
> ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
> ##                                                            ##
> ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
> ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
> ##                                                            ##
> ################################################################
>
>


#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


Re: Variable Number of Column in a Table

by Ron Savage :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Joshua

> Let me know if the attachments don't come through and you want a copy.

No sign of attachments. Can you put them on your web site?
--
Ron Savage
ron@...
http://savage.net.au/index.html



#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


Re: Variable Number of Column in a Table

by Cees Hek :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, May 16, 2008 at 2:03 AM, Stephen Carville
<stephen.carville@...> 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 ;)

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>


Cheers,

Cees

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


Re: Variable Number of Column in a Table

by Joshua Miller-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sure...
http://moc.rellimhsoj.com/perl/tmplloop_benchmark/

--
Josh I.

On Thu, May 15, 2008 at 7:07 PM, Ron Savage <ron@...> wrote:

> Hi Joshua
>
> > Let me know if the attachments don't come through and you want a copy.
>
> No sign of attachments. Can you put them on your web site?
> --
> Ron Savage
> ron@...
> http://savage.net.au/index.html
>
>
>
> #####  CGI::Application community mailing list  ################
> ##                                                            ##
> ##  To unsubscribe, or change your message delivery options,  ##
> ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
> ##                                                            ##
> ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
> ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
> ##                                                            ##
> ################################################################
>
>

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


Re: Variable Number of Column in a Table

by Joshua Miller-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

FWIW, I updated the benchmarks to include that one. While not as slow as the
nested loops in HTML::Template, it slower than the single loop in
HTML::Template.

http://moc.rellimhsoj.com/perl/tmplloop_benchmark/

Benchmark: timing 40 iterations of Templateloop, perl, tmpl1loop, tmpl2loops...
Templateloop: 18 wallclock secs (17.10 usr +  0.95 sys = 18.05 CPU) @
2.22/s (n=40)
      perl:  0 wallclock secs ( 0.68 usr +  0.00 sys =  0.68 CPU) @
58.82/s (n=40)
            (warning: too few iterations for a reliable count)
 tmpl1loop: 12 wallclock secs (11.78 usr +  0.06 sys = 11.84 CPU) @
3.38/s (n=40)
tmpl2loops: 41 wallclock secs (40.41 usr +  0.08 sys = 40.49 CPU) @
0.99/s (n=40)

Any other examples to throw in here?
--
Josh I.

On Thu, May 15, 2008 at 7:40 PM, Cees Hek <ceeshek@...> wrote:

> On Fri, May 16, 2008 at 2:03 AM, Stephen Carville
> <stephen.carville@...> 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 ;)
>
> 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>
>
>
> Cheers,
>
> Cees
>
> #####  CGI::Application community mailing list  ################
> ##                                                            ##
> ##  To unsubscribe, or change your message delivery options,  ##
> ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
> ##                                                            ##
> ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
> ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
> ##                                                            ##
> ################################################################
>
>

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


Re: Variable Number of Column in a Table

by Mark Fuller-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, May 15, 2008 at 9:03 AM, Stephen Carville
<stephen.carville@...> wrote:
> Is there any way to have a template where the number of columns is
> determined at run time?

I don't know if this is a good idea, but you can use HTML::Template's
"filter" option to modify the template's content when it's loaded. In
the template you could markup the row so it could be processed by the
filter (to repeat the <td></td> as many times as necessary).

Mark

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


Re: Variable Number of Column in a Table

by Joshua Miller-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Based on some off-list suggestions, and some hunches, I added a few more
tests.
It has been expanded a little more to include HTML::Template,
HTML::Template::Pro, and TT, and versions that read from file and ones that
use a scalar variable to hold template data (to remove the file read from
the equation).

I think that covers a pretty wide range of options, and I'm done tweaking
the tests. If anyone wants to see any others added, feel free to send me a
patch, and I'll update it.

Benchmark: timing 40 iterations of HT1loop_cachedtmpl,
HT1loop_fromfile, HT2loops_cachedtmpl, HT2loops_fromfile,
HTPro1loop_cachedtmpl, HTPro1loop_fromfile, HTPro2loops_cachedtmpl,
HTPro2loops_fromfile, Templateloop_cachedtmpl, Templateloop_fromfile,
perl...
     HT1loop_cachedtmpl: 12 wallclock secs (11.72 usr +  0.05 sys =
11.77 CPU) @  3.40/s (n=40)
       HT1loop_fromfile: 12 wallclock secs (11.74 usr +  0.06 sys =
11.80 CPU) @  3.39/s (n=40)
    HT2loops_cachedtmpl: 40 wallclock secs (39.82 usr +  0.06 sys =
39.88 CPU) @  1.00/s (n=40)
      HT2loops_fromfile: 40 wallclock secs (39.79 usr +  0.08 sys =
39.87 CPU) @  1.00/s (n=40)
  HTPro1loop_cachedtmpl:  5 wallclock secs ( 4.40 usr +  0.46 sys =
4.86 CPU) @  8.23/s (n=40)
    HTPro1loop_fromfile:  5 wallclock secs ( 4.42 usr +  0.50 sys =
4.92 CPU) @  8.13/s (n=40)
 HTPro2loops_cachedtmpl: 10 wallclock secs ( 9.55 usr +  0.73 sys =
10.28 CPU) @  3.89/s (n=40)
   HTPro2loops_fromfile: 11 wallclock secs ( 9.74 usr +  0.76 sys =
10.50 CPU) @  3.81/s (n=40)
Templateloop_cachedtmpl: 16 wallclock secs (16.38 usr +  0.16 sys =
16.54 CPU) @  2.42/s (n=40)
  Templateloop_fromfile: 19 wallclock secs (17.45 usr +  0.98 sys =
18.43 CPU) @  2.17/s (n=40)
                   perl:  0 wallclock secs ( 0.68 usr +  0.01 sys =
0.69 CPU) @ 57.97/s (n=40)

--
Josh I.

On Thu, May 15, 2008 at 7:59 PM, Joshua Miller <unrtst@...> wrote:

> FWIW, I updated the benchmarks to include that one. While not as slow as
> the nested loops in HTML::Template, it slower than the single loop in
> HTML::Template.
>
> http://moc.rellimhsoj.com/perl/tmplloop_benchmark/
>
> Benchmark: timing 40 iterations of Templateloop, perl, tmpl1loop, tmpl2loops...
> Templateloop: 18 wallclock secs (17.10 usr +  0.95 sys = 18.05 CPU) @  2.22/s (n=40)
>       perl:  0 wallclock secs ( 0.68 usr +  0.00 sys =  0.68 CPU) @ 58.82/s (n=40)
>
>             (warning: too few iterations for a reliable count)
>  tmpl1loop: 12 wallclock secs (11.78 usr +  0.06 sys = 11.84 CPU) @  3.38/s (n=40)
> tmpl2loops: 41 wallclock secs (40.41 usr +  0.08 sys = 40.49 CPU) @  0.99/s (n=40)
>
> Any other examples to throw in here?
> --
> Josh I.
>
>
> On Thu, May 15, 2008 at 7:40 PM, Cees Hek <ceeshek@...> wrote:
>
>> On Fri, May 16, 2008 at 2:03 AM, Stephen Carville
>> <stephen.carville@...> 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 ;)
>>
>> 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>
>>
>>
>> Cheers,
>>
>> Cees
>>
>> #####  CGI::Application community mailing list  ################
>> ##                                                            ##
>> ##  To unsubscribe, or change your message delivery options,  ##
>> ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
>> ##                                                            ##
>> ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
>> ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
>> ##                                                            ##
>> ################################################################
>>
>>
>

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


Re: Variable Number of Column in a Table

by Stephen Carville-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 5/15/08, Cees Hek <ceeshek@...> wrote:

> On Fri, May 16, 2008 at 2:03 AM, Stephen Carville
> <stephen.carville@...> 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

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################

LightInTheBox - Buy quality products at wholesale price