package HTML::Tabulate;
use 5.005;
use Carp;
use URI::Escape;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $TITLE_HEADING_LEVEL);
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw(&render);
$VERSION = '0.32';
my $DEFAULT_TEXT_FORMAT = "
text, or legacy text after end table tag
sub caption {
my $self = shift;
my ($set, $post_table) = @_;
my $defn_t = $self->{defn_t};
# Legacy text must have a 'format' element
if ($post_table &&
(ref $defn_t->{caption} ne 'HASH' ||
! $defn_t->{caption}->{type} ||
$defn_t->{caption}->{type} ne 'caption_caption')) {
$self->unchomp($self->text_element('caption', $set));
}
elsif (! $post_table &&
(ref $defn_t->{caption} eq 'HASH' &&
$defn_t->{caption}->{type} &&
$defn_t->{caption}->{type} eq 'caption_caption')) {
delete $defn_t->{caption}->{format}
if ($defn_t->{caption}->{format} || '') eq $DEFAULT_TEXT_FORMAT;
$self->unchomp(
$self->start_tag('caption') .
$self->text_element('caption', $set) .
$self->end_tag('caption')
)
}
}
# ------------------------------------------------------------------------
# Content before begin table tag
sub pre_table
{
my $self = shift;
my ($set) = @_;
my $content = '';
$content .= $self->title($set) if $self->{defn_t}->{title};
$content .= $self->text($set) if $self->{defn_t}->{text};
return $content;
}
# Provided for subclassing
sub start_table
{
my $self = shift;
return '' if exists $self->{defn_t}->{table} && ! $self->{defn_t}->{table};
return $self->start_tag('table',$self->{defn_t}->{table}) . "\n";
}
# Provided for subclassing
sub end_table
{
my $self = shift;
return '' if exists $self->{defn_t}->{table} && ! $self->{defn_t}->{table};
return $self->end_tag('table') . "\n";
}
# Content after end table tag
sub post_table
{
my $self = shift;
my ($set) = @_;
my $content = '';
$content .= $self->caption($set, 'post_table');
return $content;
}
# ------------------------------------------------------------------------
# Apply 'format' formatting
sub cell_format_format
{
my ($self, $data, $fattr, $row, $field) = @_;
my $ref = ref $fattr->{format};
croak "[cell_format] invalid '$field' format: $ref" if $ref && $ref ne 'CODE';
$data = &{$fattr->{format}}($data, $row || {}, $field) if $ref eq 'CODE';
$data = sprintf $fattr->{format}, $data if ! $ref;
return $data;
}
# Simple tag escaping
sub cell_format_escape
{
my ($self, $data) = @_;
$data =~ s/</g;
$data =~ s/>/>/g;
return $data;
}
# Link formatting
sub cell_format_link
{
my ($self, $data, $fattr, $row, $field, $data_unformatted) = @_;
my $ldata;
my $ref = ref $fattr->{link};
croak "[cell_format] invalid '$field' link: $ref"
if $ref && $ref ne 'CODE';
$ldata = &{$fattr->{link}}($data_unformatted, $row || {}, $field)
if $ref eq 'CODE';
$ldata = sprintf $fattr->{link}, $data_unformatted
if ! $ref;
if ($ldata) {
# $data = sprintf qq(%s),
# uri_escape($ldata, $URI_ESCAPE_CHARS), $data;
my $link_attr = { href => uri_escape($ldata, $URI_ESCAPE_CHARS)};
for my $attr (keys %$fattr) {
if ($attr =~ m/^link_/) {
my $val = $fattr->{$attr};
$attr =~ s/^link_//;
$link_attr->{$attr} = ref $val eq 'CODE' ?
$val->($data_unformatted, $row || {}, $field) :
$val;
}
}
$data = $self->start_tag('a', $link_attr) . $data . $self->end_tag('a');
}
return $data;
}
#
# Format the given data item using formatting field attributes (e.g. format,
# link, escape etc.)
#
sub cell_format
{
my $self = shift;
my ($data, $fattr, $row, $field) = @_;
my $defn = $self->{defn_t};
# Trim
$data =~ s/^\s*(.*?)\s*$/$1/ if $defn->{trim};
if ($data ne '') {
my $data_unformatted = $data;
# 'escape' boolean for simple tag escaping (defaults to on)
$data = $self->cell_format_escape($data)
if $fattr->{escape} || ! exists $fattr->{escape};
# 'format' subroutine or sprintf pattern
$data = $self->cell_format_format(@_)
if $fattr->{format};
# 'link' subroutine or sprintf pattern
$data = $self->cell_format_link($data, $fattr, $row, $field, $data_unformatted)
if $fattr->{link};
}
# 'null' defaults
$data = $defn->{null} if defined $defn->{null} && $data eq '';
return $data;
}
sub label
{
my ($self, $label, $field) = @_;
# Use first label if arrayref
my $l;
if (ref $label eq 'CODE') {
$l = $label->($field);
}
else {
$l = $label;
}
$l = $self->derive_label($field) unless defined $l;
$l = $self->{defn_t}->{null} if $l eq '' && defined $self->{defn_t}->{null};
return $l;
}
#
# Add in any extra (conditional) defaults for this field.
# Provided for subclassing.
#
sub cell_merge_extras
{
return ();
}
#
# Merge default and field attributes once each for labels and data
#
sub cell_merge_defaults
{
my ($self, $row, $field) = @_;
$self->{defn_t}->{field_attr}->{$field} ||= {};
# Create a temp $fattr hash merging defaults, regexes, and field attrs
my $fattr = { %{$self->{defn_t}->{field_attr}->{-defaults}},
$self->cell_merge_extras($row, $field) };
for my $regex (sort keys %{$self->{defn_t}->{field_attr}->{-regex}}) {
next unless $field =~ $regex;
@$fattr{ keys %{$self->{defn_t}->{field_attr}->{-regex}->{$regex}} } =
values %{$self->{defn_t}->{field_attr}->{-regex}->{$regex}};
}
@$fattr{ keys %{$self->{defn_t}->{field_attr}->{$field}} } =
values %{$self->{defn_t}->{field_attr}->{$field}};
# For labels, keep only label_ attributes
if (! defined $row) {
my $fattr_l = {};
for (keys %$fattr) {
next if substr($_,0,6) ne 'label_';
$fattr_l->{substr($_,6)} = $fattr->{$_};
}
# Set 'value' from 'label'
$fattr_l->{value} = $self->label($fattr->{label}, $field);
# Update fattr
$fattr = $fattr_l;
}
# For data, merge defaults and the field attributes, discarding label_ ones
else {
# Remove all label_ attributes
for (keys %$fattr) { delete $fattr->{$_} if substr($_,0,6) eq 'label_'; }
}
# Create tx_attr by removing all $fattr attributes in $field_attr
my %tx_attr = %$fattr;
for (keys %tx_attr) {
delete $tx_attr{$_} if exists $self->{field_attr}->{$_};
delete $tx_attr{$_} if m/^link_/;
}
# If data, save for subsequent rows
if ($row) {
$fattr->{td_attr} = \%tx_attr;
$self->{defn_t}->{field_attr}->{$field} = $fattr;
}
# Check %tx_attr for code values
my $tx_code = 0;
for my $v (values %tx_attr) {
if (ref $v eq 'CODE') {
$tx_code = 1;
$self->{defn_t}->{field_attr}->{$field}->{td_code} = 1 if $row;
last;
}
}
return ($fattr, \%tx_attr, $tx_code);
}
#
# Set and format the data for a single (data) cell or item
#
sub cell_value
{
my $self = shift;
my ($row, $field, $fattr) = @_;
my $defn = $self->{defn_t};
# Get value from $row
my $value;
if (ref $row eq 'ARRAY') {
my $i = keys %{$defn->{field_map}} ? $defn->{field_map}->{$field} : $field;
$value = $row->[ $i ] if defined $i;
}
# Allow field-methods e.g. Class::DBI
elsif (ref $row && ref $row ne 'HASH' && ref $row ne 'SCALAR' && $row->can($field)) {
$value = eval "\$row->$field()";
}
elsif (ref $row && exists $row->{$field}) {
$value = $row->{$field};
}
# 'value' literal or subref takes precedence over row
if (exists $fattr->{value}) {
my $ref = ref $fattr->{value};
if (! $ref) {
# $value = sprintf $fattr->{value}, $value;
$value = $fattr->{value};
}
elsif ($ref eq 'CODE') {
$value = &{$fattr->{value}}($value, $row, $field);
}
else {
croak "[cell_value] invalid '$field' value: $ref";
};
}
$value = $fattr->{default} if ! defined $value && exists $fattr->{default};
return defined $value ? $value : '';
}
#
# Return a cell value created from one or more other cells
#
sub cell_composite
{
my $self = shift;
my ($row, $field, $fattr) = @_;
my $composite = $fattr->{composite}
or die "Missing composite field attribute";
my @composite = ();
for my $f (@$composite) {
push @composite, $self->cell($row, $f, undef, undef, tags => 0);
}
my $composite_join = $fattr->{composite_join} || ' ';
if (ref $composite_join eq 'CODE') {
return $composite_join->(\@composite, $row, $field);
}
else {
return join ' ', @composite;
}
}
#
# Set and format the data for a single (data) cell or item
#
sub cell_content
{
my $self = shift;
my ($row, $field, $fattr) = @_;
my $value;
# Composite fields - concatenate members together
if ($fattr->{composite}) {
$value = $self->cell_composite(@_);
}
# Standard field - get value from $row
else {
$value = $self->cell_value(@_);
}
# Format
my $fvalue = $self->cell_format($value, $fattr, $row, $field);
return wantarray ? ($fvalue, $value) : $fvalue;
}
#
# Wrap cell in or | table tags
#
sub cell_tags
{
my ($self, $data, $row, $field, $tx_attr) = @_;
my $tag = ! defined $row ? 'th' : 'td';
$data = '' unless defined $data;
return $self->start_tag($tag, $tx_attr) . $data . $self->end_tag($tag);
}
#
# Execute any th or td attribute subrefs
#
sub cell_tx_execute
{
my $self = shift;
my ($tx_attr, $value, $row, $field) = @_;
my %tx2 = ();
while (my ($k,$v) = each %$tx_attr) {
if (ref $v eq 'CODE') {
$tx2{$k} = $v->($value, $row, $field);
}
else {
$tx2{$k} = $v;
}
}
return \%tx2;
}
#
# Render a single table cell or item
#
sub cell
{
my ($self, $row, $field, $fattr, $tx_attr, %opts) = @_;
my $tags = delete $opts{tags};
$tags = 1 unless defined $tags;
# Merge default and field attributes first time through (labels + data)
my $tx_code = 0;
unless ($fattr && $tx_attr) {
if (! defined $row || ! $self->{defn_t}->{field_attr}->{$field}->{td_attr}) {
($fattr, $tx_attr, $tx_code) = $self->cell_merge_defaults($row, $field);
}
else {
$fattr = $self->{defn_t}->{field_attr}->{$field};
$tx_attr = $fattr->{td_attr};
$tx_code = $fattr->{td_code};
}
}
# Standard (non-composite) fields
my ($fvalue, $value) = $self->cell_content($row, $field, $fattr);
# If $tx_addr includes coderefs, execute them
$tx_attr = $self->cell_tx_execute($tx_attr, $value, $row, $field)
if $tx_code;
# Generate tags
return $tags ? $self->cell_tags($fvalue, $row, $field, $tx_attr) : $fvalue;
}
#
# Modify the $tr hashref for striping. If $type is 'SCALAR', the stripe is
# a HTML colour string for a bgcolor attribute for the relevant row; if
# $type is 'HASH' the stripe is a set of attributes to be merged.
# $stripe has already been coerced to an arrayref if something else.
#
sub stripe
{
my ($self, $tr, $rownum) = @_;
my $stripe = $self->{defn_t}->{stripe};
return $tr unless $stripe;
my $r = int($rownum % scalar(@$stripe)) - 1;
if (defined $stripe->[$r]) {
if (! ref $stripe->[$r]) {
# Set bgcolor to stripe (exception: header where bgcolor already set)
$tr->{bgcolor} = $stripe->[$r]
unless $rownum == 0 && exists $tr->{bgcolor};
}
elsif (ref $stripe->[$r] eq 'HASH') {
# Existing attributes take precedence over stripe ones for header
if ($rownum == 0) {
for (keys %{$stripe->[$r]}) {
$tr->{$_} = $stripe->[$r]->{$_} unless exists $tr->{$_};
}
}
# For non-header rows, merge attributes straight into $tr
else {
@$tr{keys %{$stripe->[$r]}} = values %{$stripe->[$r]};
}
}
# Else silently ignore
}
return $tr;
}
#
# Return tbody close and/or open tags if appropriate, '' otherwise
#
sub tbody
{
my $self = shift;
my ($row, $rownum) = @_;
my $generate = 0;
return '' unless $self->{defn_t}->{tbody};
# Scalar tbody - generate once only
if (! ref $self->{defn_t}->{tbody}) {
$generate++ if ! $self->{defn_t}->{tbody_open};
}
# tbody with -field - generate when field value changes
elsif ($self->{defn_t}->{tbody}->{'-field'}) {
my $value = $self->cell_value($row, $self->{defn_t}->{tbody}->{'-field'});
if (exists $self->{defn_t}->{tbody_field_value}) {
if ($value eq $self->{defn_t}->{tbody_field_value} ||
(! defined $value &&
! defined $self->{defn_t}->{tbody_field_value})) {
return '';
}
else {
$generate++;
}
}
else {
$generate++;
}
$self->{defn_t}->{tbody_field_value} = $value;
}
# tbody with -rows - generate when $rownum == $r ** n + 1
elsif (my $r = $self->{defn_t}->{tbody}->{'-rows'}) {
$generate++ if int(($rownum-1) % $r) == 0;
}
# else a hashref - treat like a scalar
else {
$generate++ if ! $self->{defn_t}->{tbody_open};
}
my $tbody = '';
if ($generate) {
if ($self->{defn_t}->{tbody_open}) {
$tbody .= $self->end_tag('tbody') . "\n";
}
$tbody .= $self->start_tag('tbody', $self->{defn_t}->{tbody_attr}) . "\n";
$self->{defn_t}->{tbody_open} = 1;
}
return $tbody;
}
#
# Return an attribute hash for table rows
#
sub tr_attr
{
my ($self, $rownum, $row, $dataset) = @_;
my $defn_t = $self->{defn_t};
my $tr = undef;
if ($rownum == 0) {
$tr = $defn_t->{thtr} if $defn_t->{thtr};
$tr ||= $self->deepcopy($defn_t->{tr_base});
}
else {
if (ref $defn_t->{tr} eq 'CODE' && $row) {
$tr = $defn_t->{tr}->($row, $dataset);
}
else {
$defn_t->{tr} = {} unless ref $defn_t->{tr} eq 'HASH';
$tr = $self->deepcopy($defn_t->{tr});
# Evaluate any code attributes
$tr ||= {};
while (my ($k,$v) = each %$tr) {
$tr->{$k} = $v->($row, $dataset) if ref $v eq 'CODE';
}
}
}
# Stripe and return
return $self->stripe($tr, $rownum);
}
#
# Render a single table row (style 'down')
#
sub row_down
{
my ($self, $row, $rownum) = @_;
# Render cells
my @cells = ();
for my $f (@{$self->{defn_t}->{fields}}) {
push @cells, $self->cell($rownum == 0 ? undef : $row, $f);
}
# Build the row
my $out = '';
$out .= $self->start_tag('tr', $self->tr_attr($rownum, $row));
$out .= join('',@cells);
$out .= $self->end_tag('tr');
$out .= "\n";
return $out;
}
#
# Return a generalised iterator function to walk the set, returning undef at eod
#
sub data_iterator
{
my ($self, $set, $fields) = @_;
my $row = 0;
croak "invalid Tabulate data type '$set'" unless ref $set;
if (ref $set eq 'CODE') {
return sub {
$row = $row ? $set->() : ($self->{prefetch} || $set->());
};
}
elsif (UNIVERSAL::isa($set,'UNIVERSAL') &&
$set->can('First') && $set->can('Next')) {
return sub {
$row = $row ? $set->Next : ($self->{prefetch} || $set->First);
};
}
elsif (UNIVERSAL::isa($set,'UNIVERSAL') &&
$set->can('first') && $set->can('next')) {
return sub {
$row = $row ? $set->next : ($self->{prefetch} || $set->first);
};
}
elsif (ref $set eq 'ARRAY') {
return sub {
return undef if $row > $#$set;
$set->[$row++];
};
}
elsif (ref $set eq 'HASH' || eval { keys %$set }) {
# Check first value - drill down further unless non-reference
my $k = $fields->[0] || (sort keys %$set)[0];
# For hashes of scalars, just return the hash once-only
if (! ref $set->{$k}) {
return sub {
return undef if $row++;
$set;
};
}
# For hashes of refs, return the refs in key order
else {
return sub {
my @k = sort keys %$set;
return undef if $row > $#k;
return $k[$row++];
};
}
}
else {
croak "invalid Tabulate data type '$set'";
}
}
#
# Render the table body with successive records down the page
#
sub body_down
{
my ($self, $set) = @_;
my $body = '';
# Get data_iterator
my @fields = @{$self->{defn_t}->{fields}}
if ref $self->{defn_t}->{fields} eq 'ARRAY';
my $data_next = $self->data_iterator($set, \@fields);
# Labels/headings
if ($self->{defn_t}->{labels} && @fields) {
$body .= $self->start_tag('thead', $self->{defn_t}->{thead}) . "\n"
if $self->{defn_t}->{thead};
$body .= $self->row_down(undef, 0);
if ($self->{defn_t}->{thead}) {
$body .= $self->end_tag('thead') . "\n";
$self->{defn_t}->{thead} = 0;
}
}
elsif ($self->{defn_t}->{thead}) {
# If thead set and labels isn't, use the first data row
my $row = $data_next->();
if ($row) {
$body .= $self->start_tag('thead', $self->{defn_t}->{thead}) . "\n";
$body .= $self->row_down($row, 1);
$body .= $self->end_tag('thead') . "\n";
}
}
# Table body
my $rownum = 1;
while (my $row = $data_next->()) {
$body .= $self->tbody($row, $rownum);
$body .= $self->row_down($row, $rownum);
$rownum++;
}
if (my $data_append = $self->{defn_t}->{data_append}) {
for my $row (@$data_append) {
$body .= $self->tbody($row, $rownum);
$body .= $self->row_down($row, $rownum);
$rownum++;
}
}
$body .= $self->end_tag('tbody') . "\n" if $self->{defn_t}->{tbody_open};
return $body;
}
#
# Render a single table row (style 'across')
#
sub row_across
{
my ($self, $data, $rownum, $field) = @_;
my @cells = ();
my @across_row = ();
# Label/heading
if ($self->{defn_t}->{labels}) {
push @cells, $self->cell(undef, $field);
push @across_row, $self->cell(undef, $field, undef, undef, tags => 0);
}
# Data
for my $row (@$data) {
push @cells, $self->cell($row, $field);
push @across_row, $self->cell_value($row, $field);
}
# Build row
my $out = $self->start_tag('tr', $self->tr_attr($rownum, $data, \@across_row));
$out .= join('', @cells);
$out .= $self->end_tag('tr') . "\n";
}
sub get_dataset
{
my ($self, $set) = @_;
# Fetch the full data set
my @data = ();
croak "invalid Tabulate data type '$set'" unless ref $set;
if (ref $set eq 'CODE') {
while (my $row = $set->()) {
push @data, $row;
}
}
elsif (UNIVERSAL::isa($set,'UNIVERSAL') &&
$set->can('First') && $set->can('Next')) {
my $row = $set->First;
if (ref $row) {
do {
push @data, $row;
}
while ($row = $set->Next);
}
}
elsif (UNIVERSAL::isa($set,'UNIVERSAL') &&
$set->can('first') && $set->can('next')) {
my $row = $set->first;
if (ref $row) {
do {
push @data, $row;
}
while ($row = $set->next);
}
}
elsif (ref $set eq 'ARRAY') {
@data = @$set;
}
elsif (ref $set eq 'HASH' || eval { keys %$set }) {
@data = ( $set );
}
else {
croak "[body_across] invalid Tabulate data type '$set'";
}
return @data;
}
#
# Render the table body with successive records across the page
# (i.e. fields down the page)
#
sub body_across
{
my ($self, $set) = @_;
# Iterate over fields (instead of data rows)
my @data = $self->get_dataset($set);
my $rownum = 1;
my $body = '';
for my $field (@{$self->{defn_t}->{fields}}) {
$body .= $self->row_across(\@data, $rownum, $field);
$rownum++;
}
return $body;
}
# -------------------------------------------------------------------------
sub render_table
{
my ($self, $set) = @_;
my $defn_t = $self->{defn_t};
# Style-specific bodies (default is 'down')
my $body;
if ($defn_t->{style} eq 'down') {
$body .= $self->body_down($set);
}
elsif ($defn_t->{style} eq 'across') {
$body .= $self->body_across($set);
}
else {
croak sprintf "[render] invalid style '%s'", $defn_t->{style};
}
# Build table
my $table = '';
$table .= $self->pre_table($set);
$table .= $self->start_table();
$table .= $self->caption($set);
$table .= $body;
$table .= $self->end_table();
$table .= $self->post_table($set);
return $table;
}
#
# Render the data set $set using the settings in $self->{defn} + $defn,
# returning the resulting string.
#
sub render
{
my ($self, $set, $defn) = @_;
$set = {} unless ref $set;
# If $self is not blessed, this is a procedural call, $self is $set
if (ref $self eq 'HASH' || ref $self eq 'ARRAY') {
$defn = $set;
$set = $self;
$self = __PACKAGE__->new($defn);
undef $defn;
}
# If $defn defined, merge with $self->{defn} for this render only
if (ref $defn eq 'HASH' && keys %$defn) {
$defn = $self->merge($self->{defn}, $defn);
$self->prerender_munge($set, $defn);
}
else {
$self->prerender_munge($set);
}
$self->render_table($set);
}
# -------------------------------------------------------------------------
1;
__END__
=head1 NAME
HTML::Tabulate - HTML table rendering class
=head1 SYNOPSIS
use HTML::Tabulate qw(render);
# Setup a simple table definition hashref
$table_defn = {
table => { border => 0, cellpadding => 0, cellspacing => 3 },
th => { class => 'foobar' },
null => ' ',
labels => 1,
stripe => '#cccccc',
};
# Render a dataset using this table definition (procedural version)
print render($dataset, $table_defn);
# Object-oriented version
$t = HTML::Tabulate->new($table_defn);
print $t->render($dataset);
# Setup some dataset specific settings
$table_defn2 = {
fields => [ qw(emp_id name title edit new_flag) ],
field_attr => {
# format employee ids, add a link to employee page
emp_id => {
format => '%-05d',
link => "emp.html?id=%s",
link_target => '_blank',
align => 'right',
},
# uppercase all names
qr/name$/ => { format => sub { uc(shift) } },
# highlight new employees
new_flag => {
class => sub {
my ($data, $row, $field) = @_;
$data =~ m/^y$/i ? 'new' : 'old';
},
},
},
};
# Render the table using the original and additional settings
print $t->render($data, $table_defn2);
=head1 DESCRIPTION
HTML::Tabulate is used to render/display a given set of data in an
HTML table. It takes a data set and a presentation definition and
applies the presentation to the data set to produce the HTML table
output. The presentation definition accepts arguments corresponding
to HTML table tags ('table', 'tr', 'th', 'td' etc.), to define
attributes for those tags, plus additional arguments for other
aspects of the presentation. HTML::Tabulate supports advanced
features like automatic striping, arbitrary cell formatting,
link creation, etc.
Presentation definitions can be defined in multiple passes, which
are progressively merged, allowing general defaults to be defined
in common and then overridden by more specific requirements.
Presentation definitions are stored in the current object, except
for those defined for a specific 'render', which are temporary.
Supported data sets include arrayrefs of arrayrefs (DBI
selectall_arrayref, for example), arrayrefs of hashrefs, a simple
hashref (producing single row tables), iterator objects that
support first() and next() methods (like DBIx::Recordset objects or
Class::DBI/DBIx::Class iterators), and (as of version 0.31)
iterator subroutines returning successive rows in the dataset.
By default arrayref-based datasets are interpreted as containing
successive table rows; a column-based interpretation can be forced
using style => 'across'.
The primary interface is object-oriented, but a procedural
interface is also available where the extra flexibility of the OO
interface is not required.
=head2 PRESENTATION DEFINITION ARGUMENTS
=over 4
=item table
Hashref. Elements become attributes on the tag. e.g.
table => { border => 0, cellpadding => 3, align => 'center' }
=item tr
Hashref. Elements become attributes on tags. Element values
may be either scalars, which are used as literals, or subroutine
references whose result value is used as the value of the tr
attribute.
Note that 'tr' element subs are called differently depending on
the 'style' of the table. For 'down' style tables, they are called
with a single argument:
$sub->( $data_row )
which is the reference to the current data row. For 'across' style
tables, they are called with two arguments:
$sub->( $across_row, $data )
where the first is an arrayref of the values in the data slice
(column) in your dataset that are being rendered as the current
row (including labels, if used), and the second is the full dataset
as an arrayref of your data rows.
For instance:
style => 'across',
labels => 1,
tr => {
class => sub {
my $r = shift; my $name = $r->[0]; $name =~ s/\s+/_/; lc $name
},
},
will set the 'class' attribute on the 'tr' to be a lowercased
underscored version of the row label.
=item thead
Scalar/hashref. If defined and true, the first line of the table
(whether labels or data) will be wrapped in ...
tags. Any entries in the hashref will be used as attributes for
the thead tag. Note that theads require a tbody, so tbody
(following) will be set to 1 if undefined.
=item tbody
Scalar/hashref. If defined and true, the default treatment is
to wrap the table body (the non-labels portion of the table)
in a single set of .. tags. Any entries in the
hashref (except for '-field' and '-rows', used below) will be
used as attributes for the tbody tag.
Two additional tbody styles are supported. If a '"-field" =>
"FIELDNAME"' element exists in the tbody hashref, then the table
body will be broken into tbody sections whenever the value of the
given field changes (does not necessarily need to be a
B field, of course) e.g.
tbody => { '-field' => 'emp_gender' }
If a '"-rows" => NUMBER' element exists in the tbody hashref, the
table body will be broken into tbody sections every NUMBER rows.
e.g.
tbody => { '-rows' => 25 }
=item thtr
Hashref. Elements become attributes on the tag of the
label/heading row. (For 'across' style tables, where labels are
displayed down the page, rather than in a row, thtr elements
become attributes of the individual | tags.) Element values
must be scalars.
=item th
Hashref. Elements become attributes on the | tags used for
labels/headings. Element values may be either scalars, which are
used as literals, or subroutine references, which are called with
the following arguments:
$sub->( $data, $row, $field )
and the result used as the attribute value. The arguments are:
$data is the (label) value; $row is a reference to the entire
row; and $field is the name of the field (so subreferences can
be potentially used for more than one field).
For example, given the following set of labels on a table:
'Emp ID', 'Emp Name', 'Emp Title', 'Emp Birth Dt'
you could define a class attribute to the | tag by doing:
th => {
class => sub {
my ($d, $r, $f) = @_;
$d =~ m/^Emp //;
$d =~ m/\s+/_/g;
lc $d
},
}
which would give a th line like (line breaks added for clarity):
|
| Emp ID |
Emp Name |
Emp Title |
Emp Birth Dt |
=item td
Hashref. Elements become attributes on tags. Hash values
may be either scalars, which are used directly, or subroutine
references, which are called with the following arguments:
$sub->( $data, $row, $field )
and the result used as the attribute value. See the preceding
L | item for further explanation and discussion.
=item fields
Arrayref. Defines the order in which fields are to be output for this table,
using the field names from the dataset. e.g.
fields => [ qw(emp_id emp_name emp_title emp_birth_dt) ]
If 'fields' is not defined at render time and the dataset is not array-based,
HTML::Tabulate will attempt to derive a useful default set from your data, and
croaks if it is not successful.
=item fields_add
Hashref. Used to define additional fields to be included in the output to
supplement a default field list, or fields derived from a data object itself.
The keys of the fields_add hashref are existing field names; the values are
scalar values or arrayref lists of values to be inserted into the field
list B the key field. e.g.
fields_add => {
emp_name => [ 'emp_givenname', 'emp_surname' ],
emp_birth_dt => 'edit',
}
applied to a fields list qw(emp_id emp_name emp_title emp_birth_dt)
produces a composite field list containing:
qw(emp_id emp_name emp_givenname emp_surname emp_title
emp_birth_dt edit)
=item fields_omit
Arrayref. Used to omit fields from the base field list. e.g.
fields_omit => [ qw(emp_modify_ts emp_create_ts) ]
=item in_fields
Arrayref. Defines the order in which fields are defined in the dataset, if
different to the output order defined in 'fields' above. e.g.
in_fields => [ qw(emp_id emp_title emp_birth_dt emp_title) ]
Using in_fields only makes sense if the dataset rows are arrayrefs.
=item style
Scalar, either 'down' (the default), or 'across', to render data 'rows'
as table 'columns'.
=item labels
Scalar (boolean), or hashref (mapping field keys to label/heading values).
Labels can also be defined using the 'label' attribute argument in per-field
attribute definitions (see 'label' below). e.g.
# Turn labels on, derived from field names, or defined per-field
labels => 1
=item label_links
Hashref, mapping field keys to URLs (full URLs or absolute or relative
paths) to be used as the targets when making the label for that field into
an HTML link. e.g.
labels => { emp_id => 'Emp ID' },
label_links => { emp_id => "me.html?order=%s" }
will create a label for the emp_id field of:
Emp ID
=item stripe
Scalar, arrayref, or hashref. A scalar or an arrayref of scalars should
be HTML color values. Single scalars are rendered as HTML 'bgcolor' values
on the tags of alternate rows (i.e. alternating with no bgcolor tag
rows), beginning with the label/header row, if one exists. Multiple
scalars in an arrayref are rendered as HTML 'bgcolor' values on the
tags of successive rows, cycling through the whole array before starting
at the beginning again. e.g.
# alternate grey and default bgcolor bands
stripe => '#999999'
# successive red, green, and blue stripes
stripe => [ '#cc0000', '#00cc00', '#0000cc' ]
Stripes that are hashrefs or an arrayref of hashrefs are rendered as
attributes to the tags on the rows to which they apply. Similarly
to scalars, single hashrefs are applied to every second tag, beginning
with the label/header row, while multiple hashrefs in an arrayref are applied
to successive rows, cycling though the array before beginning again. e.g.
# alternate stripe and default rows
stripe => { class => 'stripe' }
# alternating between two stripe classes
stripe => [ { class => 'stripe1' }, { class => 'stripe2' } ]
=item null
Scalar, defining a string to use in place of any empty data value (undef
or eq ''). e.g.
# Replace all empty fields with non-breaking spaces
null => ' '
=item trim
Scalar (boolean). If true, leading and trailing whitespace is removed
from data values.
=item field_attr
Hashref, defining per-field attribute definitions. Three kinds of keys are
supported:
=over 4
=item -defaults
The special literal '-defaults' is used to define defaults for all fields
(but can be overridden by more specific definitions).
=item qr() regular expressions
qr-quoted regular expressions are used as defaults for fields where the
regex matches the field name.
=item field names
Simple field names define attributes just for that field.
=back
These are always merged in the order above, allowing defaults to be
defined for all fields, overridden for fields matching particular
regexes, and then overridden further per-field. e.g.
# Align all fields left except timestamps (*_ts)
field_attr => {
-defaults => { align => 'left' },
qr/_ts$/ => { align = 'center' },
emp_create_ts => { label => 'Created' },
},
Field attribute arguments are discussed in the following section.
=item title
Scalar, hashref, or subroutine reference, defining a title rendered above
the table. A scalar title is interpreted as the title string, and rendered
as a vanilla title (use hashref or subref variants for more control).
A hashref title can contains 'value' and 'format' elements - 'value' is a
scalar containing the title string, and 'format' is a scalar sprintf
pattern (default: '%s ') used to render the title value, or a subref
called with the following arguments:
$format->($value, $dataset, $type)
(where $type is 'title') and should return the formatted title string to
be used.
Subref titles are similar, except there is no separate title string involved;
they are called with the following arguments:
$title->($dataset, $type);
(where $type is 'title') and should return the formatted title string to
be used.
Examples:
# rendered: Employee Data
title => 'Employee Data'
# rendered: Employee Data
title => {
value => 'Employee Data',
format => '%s',
}
# rendered (e.g.): Employee Data (3 records)
title => sub {
my ($set, $type) = @_;
my $title = 'Employee Data';
$title .= ' (' . scalar(@$set) . ' records)'
if ref $set eq 'ARRAY';
sprintf '%s', $title;
}
=item text
Scalar, hashref, or subroutine reference, defining text to be included
immediately before the table (but after a 'title', if any). Treated
exactly like 'title' above, except that the $type argument passed to
subrefs is 'text', and the default format defined is '%s '.
=item caption
Scalar, hashref, or subroutine reference, defining text to be included
as a caption to the table. Two types of output are supported: the 'text'
type is treated just like 'title' and 'text' above, except that the
text is included immediately B the table, the $type argument
passed to subrefs is 'caption', and the default format defined is
'%s '.
From version 0.26, a new 'caption_caption' type is supported, which
is rendered as a attribute on the table (with presentation
typically controlled via css). To force this type, you should use
a hashref caption argument, with an explicit type of 'caption_caption'.
See below for examples.
For backward compatibility, the default is old-style type => 'caption'.
That will change in a future release.
For example:
# Old style text caption, rendered below table
# rendered Employee Data (below table)
caption => 'Employee Data'
# rendered Employee Data (below table)
caption => {
value => 'Employee Data',
format => '%s ',
}
# rendered (e.g.): Employee Data (3 records) (below table)
caption => sub {
my ($set, $type) = @_;
my $caption = 'Employee Data';
$caption .= ' (' . scalar(@$set) . ' records)'
if ref $set eq 'ARRAY';
sprintf '%s ', $caption;
}
# New-style caption, rendered within table
# rendered Employee Data (within table)
caption => {
type => 'caption_caption',
value => 'Employee Data',
}
# rendered (e.g.): Employee Data (3 records) (within table)
caption => {
type => 'caption_caption',
value => 'Employee Data',
format => sub {
my ($caption, $set, $type) = @_;
$caption .= ' (' . scalar(@$set) . ' records)'
if ref $set eq 'ARRAY';
$caption
}
}
=item data_append
Array reference containing supplementary data rows to be appended to the table
after the main dataset. data_append rows are currently treated exactly the same
as main data rows.
=back
=head2 FIELD ATTRIBUTE ARGUMENTS
=over 4
=item HTML attributes
Any field attribute that does not have a special meaning to HTML::Tabulate
(see the six remaining items in this section) is considered an HTML attribute
and is used with the tag for table cells for this field. e.g.
field_attr => {
emp_id => {
align => 'center',
valign => 'top',
class => sub { my ($d, $r, $f) = @_; $f =~ s/^emp_//; $f },
}
}
will cause emp_id table cells to be displayed as:
|
Attribute values may be either scalar, which are used directly, or
subroutine references, which are called with the following arguments:
$sub->( $data, $row, $field )
and the result used as the attribute value. The arguments are:
the (unformatted) data value; a reference to the entire data row; and
the field name (so subreferences can be potentially used for more than
one field).
=item value
Scalar or subroutine reference. Used to override or modify the current
data value. If scalar is taken as a literal. If a subroutine reference,
is called with the following arguments:
$sub->( $data, $row, $field )
and the result used as the data value. The arguments are: the original
data value itself; a reference to the entire data row; and the field
name (so subrefs can potentially be used for more than one field).
This allows the value to be modified or set according to the current
value, or based on any other value in the row (or anything else, for
that matter) e.g.
# Derive emp_fname from first word of emp_name
field_attr => {
emp_fname => {
value => sub {
my ($data, $row, $field) = @_;
if ($row->{emp_name} =~ m/^\s*(\w+)/) { return $1; }
return '';
},
},
edit => { value => 'edit' },
}
=item format
Scalar or subroutine reference. Used to format the current data value.
If scalar, is taken as a sprintf pattern, with the current data value
as the single argument. If a subroutine reference, is called in the
same way as the value subref above
i.e. $format->($data_item, $row, $field)
=item link
Scalar or subroutine reference. Used as the link target to make an
HTML link using the current data value. If scalar, the target is taken
as a sprintf pattern, with the current data value as the single argument.
If a subroutine reference, is called in the same way as the value subref
described above i.e. $link->($data, $row, $field) e.g.
field_attr => {
emp_id => {
link => 'emp.html?id=%s',
format => '%05d',
},
}
creates a link in the table cell like:
00001
Note that links are not created for labels/headings - to do so use the
separate label_link argument below.
=item link_*
Scalar or subroutine reference. Any attribute beginning with 'link_' is
used as an attribute for the HTML link created for this field (with the
'link_' prefix removed, of course). Scalar values are used as literals;
subroutine references are called in the same way as the value subref
above i.e. $attr->($data_item, $row, $field) e.g.
field_attr => {
emp_id => {
link => 'emp.html?id=%s',
link_class => sub { my ($d, $r, $f) = @_; "class_$f" },
link_target => '_blank',
link_title => 'Employee details',
},
}
creates a link in the table cell like:
123
=item label
Scalar or subroutine reference. Defines the label or heading to be used
for this field. If scalar, the value is taken as a literal (cf. 'value'
above). If a subroutine reference is called with the field name as the
only argument (typically only useful for -default or regex-based labels).
Entries in the top-level 'labels' hashref are mapped into per-field
label entries.
=item label_link
Scalar or subroutine reference. Equivalent to the general 'link'
argument above, but used to create link targets only for label/heading
rows. Scalar values are taken as sprintf patterns using the label as
argument; subroutine references are called in the same way as the value
subref above i.e. $link->($data_item, $row, $field)
=item label_link_*
Scalar or subroutine reference. Like 'link_*' attributes above, used as
attributes on the HTML link created for the label for this field.
Scalar values are used as literals; subroutine references are called in
the same way as the value subref above i.e. $attr->($data_item, $row,
$field) e.g.
field_attr => {
emp_id => {
label => 'Emp ID',
label_link => sub { my ($d, $r, $f) = @_; "?order=$f" },
label_link_target => '_blank',
label_link_title => sub { my ($d, $r, $f) = @_; "Order by $d" },
},
}
creates a link for the label like:
Emp ID
=item escape
Boolean (default true). HTML-escapes '<' and '>' characters in data
values.
=item composite
Arrayref. New as of version 0.30, composite fields define an ordered
list of other fields that you want to appear in a single cell. For
instance, given individual name fields in your data you might want to
define a composite name field to use in your table instead e.g.
field_attr => {
fullname => {
composite => [ qw(given_name middle_initial surname) ],
},
surname => {
format => sub { uc $_[0] },
},
# ...
},
Typically, the base fields appear in your data (e.g. given_name,
middle_initial, and surname) but not in your table, and your
composite field appears in the table but not in your data (but
other patterns do make sense too sometimes).
=item composite_join
Scalar or subroutine reference. If a scalar, functions as the string
used to join the rendered composite fields together. If a subroutine
reference, is called with the following arguments:
$composite_join->(\@composite_fields, $row, $field_name)
and is expected to join the composite fields itself and return the
joined string.
Default: ' '.
=back
=head2 METHODS
HTML::Tabulate has three main public methods:
=over 4
=item new($table_defn)
Takes an optional presentation definition hashref for a table, sanity
checks it (and croaks on failure), stores the definition, and returns
a blessed HTML::Tabulate object.
=item merge($table_defn)
Checks the given presentation definition (croaking on failure), and
then merges it with its internal definition, storing the result. This
allows presentation definitions to be created in multiple passes, with
general defaults overridden by more specific requirements.
=item render($dataset, $table_defn)
Takes a dataset and an optional presentation definition, creates a
merged presentation definition from any prior definitions and the
render one, and uses that merged definition to render the given
dataset, returning the HTML table produced. The merged definition
is discarded after the render; only definitions stored by the new()
and merge() methods are persistent across renders.
render() can also be used procedurally if explicitly imported:
use HTML::Tabulate qw(render);
print render($dataset, $table_defn);
=back
=head2 DATASETS
HTML::Tabulate supports the following dataset types:
=over 4
=item Simple hashrefs
A simple hashref will generate a one-row table (or one column table
if style is 'across'). Labels are derived from key names if not
supplied.
=item Arrayrefs of arrayrefs
An arrayref of arrayrefs will generate a table with one row for
each contained arrayref (or one column per arrayref if style
is 'across'). Labels cannot be derived from arrayrefs, so they
must be supplied if required.
=item Arrayrefs of hashrefs
An arrayref of hashrefs will generate a table with one row for
each hashref (or one column per hashref if style is 'across').
Labels are derived from the key names of the first hashref if
not supplied.
=item Arrayrefs of objects
An arrayref containing hash-based objects (i.e. blessed hashrefs)
are treated just like unblessed hashrefs, generating a table with
one row per object. Labels are derived from the key names of the
first object if not supplied.
=item Iterators
Some kinds of iterators (utility objects used to access the members
of a set) are also supported. If the iterator supports methods called
First() and Next() or first() and next() then HTML::Tabulate will use
those methods to walk the dataset. DBIx::Recordset objects and
Class::DBI and DBIx::Class iterators definitely work; beyond those
your mileage may vary - please let me know your successes and
failures.
As of version 0.31, HTML::Tabulate also supports generic coderef
iterators i.e. subroutines that return successive data rows on
subsequent calls to the subroutine (and undef at end of data)
e.g.
# Toy example: given an array of rows in @data
$t = HTML::Tabulate->new;
print $t->render( sub { shift @data } );
=back
=head1 SUBCLASSING
HTML::Tabulate is intended to be easy to subclass, to allow you to
setup sensible defaults for site-wide use, for instance. Something
like this seems to work well:
package My::Tabulate;
use base qw(HTML::Tabulate);
sub new {
my $class = shift;
my $defn = shift || {};
my %defaults = (
# define table defaults here e.g.
table => { border => 1 },
labels => { foo => 'FOO', bar => 'BAR' },
);
my $self = $class->SUPER::new(\%defaults);
$self->merge($defn);
return $self;
}
1;
=head1 BUGS AND CAVEATS
Probably. Please let me know if you find something going awry.
Is now much bigger and more complicated than was originally envisaged.
Needs to be completely refactored. Sometime.
=head1 AUTHOR
Gavin Carr
Contributors:
David Giller reported a bug in the generic subref
iterator handling, and provided a fix (version 0.32).
Harry Danilevsky - patch adding generic
subref iterator support (version 0.31).
=head1 COPYRIGHT
Copyright 2003-2008, Gavin Carr.
This program is free software. You may copy or redistribute it under the
same terms as perl itself.
=cut
# vim:sw=4
| | |