The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

HTML::Tabulate - HTML table rendering class

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) ],
        field_attr => {
            # format employee ids, add a link to employee page
            emp_id => {
                format => '%-05d',
                link => "emp.html?id=%s",
                align => 'right',
            },
            # upper all names
            qr/name$/ => { format => sub { uc(shift) } },
        },
    };

    # Render the table using the original and additional settings
    print $t->render($data, $table_defn2);

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), or iterator objects that support first() and next() methods (like DBIx::Recordset objects or Class::DBI iterators).

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.

PRESENTATION DEFINITION ARGUMENTS

table

Hashref. Elements become attributes on the <table> tag. e.g.

  table => { border => 0, cellpadding => 3, align => 'center' }
tr

Hashref. Elements become attributes on <tr> tags.

thtr

Hashref. Elements become attributes on the <tr> tag of the label/heading row. (For 'across' style tabels, where labels are displayed down the page, rather than in a row, thtr elements become attributes of the individual <th> tags.)

th

Hashref. Elements become attributes on the <th> tags used for labels/headings.

td

Hashref. Elements become attributes on <td> tags.

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.

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 after 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)
fields_omit

Arrayref. Used to omit fields from the base field list. e.g.

  fields_omit => [ qw(emp_modify_ts emp_create_ts) ]
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.

style

Scalar, either 'down' (the default), or 'across', to render data 'rows' as table 'columns'.

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

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:

  <a href="me.html?order=emp_id">Emp ID</a> 
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 <tr> 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 <tr> 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 <tr> tags on the rows to which they apply. Similarly to scalars, single hashrefs are applied to every second <tr> 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' } ]
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 => '&nbsp;'
trim

Scalar (boolean). If true, leading and trailing whitespace is removed from data values.

field_attr

Hashref, defining per-field attribute definitions. Three kinds of keys are supported: the special value '-defaults', used to define defaults for all fields; qr() regular expresssions, used as defaults if the regex matches the field name; and simple field names. These are merged in the order given, 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.

FIELD ATTRIBUTE ARGUMENTS

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 <td> tag for table cells for this field. e.g.

  field_attr => {
    emp_id => {
      align => 'center',
      class => 'emp_id', 
      valign => 'top',
    }
  }

will cause emp_id table cells to be displayed as:

  <td align="center" class="emp_id" valign="top">
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 three arguments: the data value itself, the entire data row, and the field name. This allows the value to be modified or set either based on an existing value, or on any other value in the row. e.g.

  # Derive emp_fname from first word of emp_name
  field_attr => {
    emp_fname => { 
      value => sub { 
        my ($fn, $row, $field) = @_; 
        if ($row->{emp_name} =~ m/^\s*(\w+)/) { return $1; }
        return '';
      },
    },
    edit => { value => 'edit' },
  }
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)

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_item, $row, $field) e.g.

  field_attr => {
    emp_id => {
      link => 'emp.html?id=%s',
      format => '%05d',
    }
  }

creates a link in the table cell like:

  <a href="emp.html?id=1">00001</a>

Note that links are not created for labels/headings - to do so use the separate label_link argument below.

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.

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; subroutine references are called with the data item and the row as the two arguments.

escape

Boolean (default true). HTML-escapes '<' and '>' characters in data values.

METHODS

HTML::Tabulate has three main public methods:

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.

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.

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);

DATASETS

HTML::Tabulate supports the following dataset types:

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.

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.

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.

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.

Iterators

Some kinds of iterators (pointer 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 iterators definitely work; beyond those your kilometerage may vary - please let me know your successes and failures.

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.

AUTHOR

Gavin Carr <gavin@openfusion.com.au>

COPYRIGHT

Copyright 2003, Gavin Carr. All Rights Reserved.

This program is free software. You may copy or redistribute it under the same terms as perl itself.