In the spirit of using HTML::Template with CGI and DBI, I am wanting to output a table that is of unknown dimensions at runtime, while still keeping the CGI portion and HTML portion as separate as possible so as to allow the 2 developer sets to work independently (and to ease maintenance as well, since we seem to have a plethora of  *gack* FP-Happy html types in our company and far too few Perl writers). 

I once wrote an article on using the HTML::Template with DBI that was a good answer for the time - but now I think I must either create my own solution, or find a better one that already exists. 

An example of my needs:

given a dataset that can be any number of rows by any number of columns, output the appropriate HTML table in the HTML template and have it render it without resorting to embedding any static HTML into my CGI Script.

The template handles a static sized dataset thusly (and quite nicely too):

my $response = HTML::Template->new( filename => $filename, path => "/$config{'template_dir'}/$theme/", die_on_bad_params => 0, global_vars => 1, associate => $request_cgi, loop_context_vars => 1, filter => {sub=> \&compress,format=> 'scalar'}, ); my $sqlquery = 'select t.field_one as DATA1, t.field_two as DATA2 from + table_one t order by t.field_one ASC'; my $sth = exec_query($sqlquery) or die("Wacka!!"); my $arrayref = $sth->fetchall_arrayref({}); $response->param(LOOPTEST => $arrayref);

along with this HTML:

<!--BEGIN Dynamic data table--> <TMPL_UNLESS NAME=LOOPTEST> <font color=red>No records found.</font> </TMPL_UNLESS> <TMPL_LOOP NAME=LOOPTEST> <TMPL_IF NAME="__FIRST__"> <table> <tr> <th bgcolor="#80D0FF"><font color="navy">Data 1</font></th> <th bgcolor="#80D0FF"><font color="navy">Data 2</font></th> </tr> </TMPL_IF> <TMPL_IF NAME="__ODD__"> <tr> <td><a href="<TMPL_VAR NAME=SYSTEM_URL>"><TMPL_VAR NAME=DATA1></ +a></td> <td><TMPL_VAR NAME=DATA2></td> </tr> <TMPL_ELSE> <tr bgcolor="silver"> <td bgcolor="silver"><a href="<TMPL_VAR NAME=SYSTEM_URL>/<TMPL_V +AR NAME=DATA1>"><TMPL_VAR NAME=DATA1></a></td> <td bgcolor="silver"><TMPL_VAR NAME=DATA2></td> </tr> </TMPL_IF> <TMPL_IF NAME="__LAST__"> </table> </TMPL_IF> </TMPL_LOOP> <!--END Dynamic data table-->

Is there a way to do this with a dynamic dataset, with changing numbers of both rows and columns?


In reply to Fetchall_Answer Returns by growlf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.