Subject: | Style table with CSS rather than bgcolor |
Data::Table::html's style arguments (bgcolor and border=1 for the table)
are marked as deprecated by the W3C. In the context of a modern
website's stylesheet it would be useful to have a method that produces
strict HTML stylable through CSS.
Basically that means switching $colors (and "bgcolor") with $classes
(and "class") and replacing table's border=1 with class="data_table".
This is enough to allow full styling of the table with CSS. I include
only two classes for the rows (odd and even) because table headers are
easy to style separately. As a side effect, the code produced could be
valid HTML 4.01 Strict.
Here is a proposed code (only minor modifications from sub html are needed):
sub html_strict {
my ($self, $classes, $tag_tbl, $tag_tr, $tag_th, $tag_td, $portrait) = @_;
my ($s, $s_tr, $s_td, $s_th) = ("", "tr", "", "th");
my $key;
$tag_tbl = { class => "data_table" } unless (ref $tag_tbl eq 'HASH');
$tag_tr = {} unless (ref $tag_tr eq 'HASH');
$tag_th = {} unless (ref $tag_th eq 'HASH');
$tag_td = {} unless (ref $tag_td eq 'HASH');
$portrait = 1 unless defined($portrait);
$s = "<table ";
foreach $key (keys %$tag_tbl) {
$s .= " $key=\"$tag_tbl->{$key}\"";
}
$s .= ">\n";
my $header=$self->{header};
my @CELL_CLASSES=("odd","even");
@CELL_CLASSES=@$classes if ((ref($classes) eq "ARRAY") && (scalar
@$classes==2));
foreach $key (keys %$tag_tr) {
$s_tr .= " $key=\"$tag_tr->{$key}\"";
}
foreach $key (keys %$tag_th) {
$s_th .= " $key=\"$tag_th->{$key}\"";
}
if ($portrait) {
$s .= "<$s_tr><$s_th>" . join("</th><$s_th>", @$header) .
"</th></tr>\n";
$self->rotate() if $self->{type};
my $data=$self->{data};
for (my $i=0; $i<=$#{$data}; $i++) {
$s .= "<$s_tr class=\"" . $CELL_CLASSES[$i%2] . "\">";
for (my $j=0; $j<=$#{$header}; $j++) {
my $s_td = $tag_td->{$j} || $tag_td->{$header->[$j]};
$s .= defined($s_td)? "<td $s_td>":"<td>";
$s .= (defined($data->[$i][$j]) && $data->[$i][$j] ne
'')?$data->[$i][$j]:" ";
$s .= "</td>";
}
$s .= "</tr>\n";
}
} else {
$self->rotate() unless $self->{type};
my $data=$self->{data};
for (my $i = 0; $i <= $#{$header}; $i++) {
$s .= "<$s_tr><$s_th>" . $header->[$i] . "</th>";
my $s_td = $tag_td->{$i} || $tag_td->{$header->[$i]};
for (my $j=0; $j<=$#{$data->[0]}; $j++) {
$s .= defined($s_td)? "<td $s_td":"<td";
$s .= " class=\"" . $CELL_CLASSES[$j%2] . "\">";
$s .= (defined($data->[$i][$j]) && $data->[$i][$j] ne
'')?$data->[$i][$j]:' ';
$s .= "</td>";
}
$s .= "</tr>\n";
}
}
$s .= "</table>\n";
return $s;
}