Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

HTML::Template create table

by GertMT (Hermit)
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.
Monday,snow Tuesday,rain Wednesday,snow Thursday,snow Friday,sunny Saturday,snow Sunday,rain

#!/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;
<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>

Replies are listed 'Best First'.
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.
    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.
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 => " +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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2022-09-27 08:49 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (118 votes). Check out past polls.