Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

HTML::Template create table

by GertMT (Friar)
on Nov 17, 2007 at 19:49 UTC ( #651443=perlquestion: print w/ replies, xml ) Need Help??
GertMT has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I'm struggling once more with HTML::Template. As an example I provide a tiny csv-file that I use as a database. I currently use Data::Table (...for all kind of shuffling that's not mentioned here...) but the HTML output that comes from the script that is shown below (SelectDataScript) is not really what I want. If I would like to use HTML::Template I understand I need to feed the data into an array of hashes but currently I'm getting nowhere. Guess my template (see below 'template_whthr.tmpl') should work but can't I use the $table object that was generated in some way?
I'm grateful for any clues.
Gert
wheather.csv
Monday,snow Tuesday,rain Wednesday,snow Thursday,snow Friday,sunny Saturday,snow Sunday,rain

SelectDataScript
#!/usr/bin/perl -w use strict; use diagnostics; use HTML::Template; use Data::Table; use DBI; # open( OUT, ">snow.html" ) or die "cant open out $!"; my $dbh = DBI->connect( 'dbi:AnyData(RaiseError=>1):' or die $DBI::err +str ); $dbh->func( 'whthr', 'CSV', 'wheather.csv', { sep_char => ',', # eol => "\015", col_names => 'day,type' }, 'ad_catalog' ); my $table = Data::Table::fromSQL( $dbh, "SELECT day, type FROM whthr WHERE type='snow'" ); print $table->html; # not the preferred formatting print $table->csv; # ? # for my $row (@rows) { # push( @loop_data, {FILES => $row} ); # } # Can I use the object $table for the $template of HTML::Template? # my $template = HTML::Template->new( filename => 'template_whthr.tmpl +' ); # print OUT $template->output;
template_whthr.tmpl
<p> <table class="center"> <TMPL_LOOP NAME=ROWS> <tr> <td> <TMPL_VAR NAME=day></td> <td> <TMPL_VAR NAME=type></td> </tr> </TMPL_LOOP> </table> </p>

Comment on HTML::Template create table
Select or Download Code
Re: HTML::Template create table
by zer (Deacon) on Nov 17, 2007 at 21:28 UTC
    you need to set up the parameters for the template.
    $template->param(ROWS=>$arrayReference);
    The array reference is a list of hashes, so if you were to type this out logicaly it would look something like this

    $arrayref->[0]->{day}=Monday $arrayref->[0]->{type}=snow $arrayref->[1]->{day}=Tuesday $arrayref->[1]->{day}=rain $arrayref->[2]->{day}=Wednesday ... etc
    Be sure to look at HTML::Template for some good examples and documentation.

    As well take a look at perldsc for a tutorial on how to make arrays of hashes.

      thanks for the very clear explanation! Together with the link to perldoc I'll be able to create that from the csv-file. Need to do some thinking again about how to start from my database processing but I'll take the challenge.
      Gert
Re: HTML::Template create table
by neniro (Priest) on Nov 18, 2007 at 11:55 UTC
    Alternatively I'd like to suggest HTML::Template::Compiled. In your script you just need to say:
    my $htc = HTML::Template::Compiled->new(filename => "template_whthr.tm +pl"); $htc->param( table => $table ); print $htc->output;
    Because it's possible to call methods on objects in HTC you just need the following in your template:
    <%= table.html %>
    If you prefer your own formatting, it's should be possible to subclass Data::Table to use an own to_html-method.
      Thanks for the suggestion. I'll try how it works as it'll improve my understanding of the module and it is more direct as what I'm currently doing. That is, I create the table from Data::Table and do:
      print OUT $table->html
      and then include it with the following in my template.
      <TMPL_INCLUDE NAME="snow.html">
      in my template.
      I'll have to see how this subclass Data::Table is working..? I'm definitely planning to use my own formatting with CSS and stuff like that.
      Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://651443]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-11-28 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (196 votes), past polls