Thanks for all input. With the code mentioned below that is inspired by your input and some article re. HTML::template on perl.com I've got HTML::template creating a nice table with odd/even rows. Just one thing to work on is how if it were not a single column table. Guess I've got to read the csv that's providing the data for the table into a hash. That'll be a challenge again.
#!/usr/bin/perl -w
use strict;
use diagnostics;
use HTML::Template;
open( OUT, ">fruit.html" ) or die "cant open out $!";
my $html = do{local $/;<DATA>};
my $tpl = HTML::Template->new(
scalarref => \$html,
loop_context_vars => 1,
filter => \&cstmrow_filter
);
my @rows = ( "Apple", "Orange", "Mango", "Kiwi" );
my @loop_data = (); # initialize an array to hold my loop
while (@rows) {
my %row_data; # a hash for my data list
$row_data{FILES} = shift @rows;
push( @loop_data, \%row_data );
}
$tpl->param( rows => \@loop_data );
print OUT $tpl->output;
sub cstmrow_filter {
my $text_ref = shift;
$$text_ref =~ s/<CSTM_ROW\s+EVEN=(.+)\s+ODD=(.*)\s*>
/<TMPL_IF NAME=__odd__>
<tr class="$1">
<TMPL_ELSE>
<tr class="$2">
<\/TMPL_IF>
/gx;
}
__DATA__
<head>
<style type="text/css">
.odd { background-color: #d3d3d3 }
.even { background-color: #90ee90 }
</style>
</head>
<body>
<p>
<table class="center">
<TMPL_LOOP NAME=rows>
<CSTM_ROW EVEN=even ODD=odd>
<td> <TMPL_VAR NAME=FILES></td>
</tr>
</TMPL_LOOP>
</table>
</p>
</body>
</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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.