usually can be found as the first child of the parent
gi_tr => 'iterate',
# the model data to be pushed into the table
table_data => $o->load_data,
# the way to take the model data and obtain one row
# if the table data were a hashref, we would do:
# my $key = (keys %$data)[0]; my $val = $data->{$key}; delete $data->{$key}
tr_data => sub { my ($self, $data) = @_;
shift(@{$data}) ;
},
# the way to take a row of data and fill the tags
td_data => sub { my ($tr_node, $tr_data) = @_;
$tr_node->content_handler($_ => $tr_data->{$_})
for qw(name age weight) }
);
print $seamstress->as_HTML;
Looping over Multiple Sample Rows
* HTML
name | age | weight |
NATURE BOY RIC FLAIR |
35 |
220 |
NATURE BOY RIC FLAIR |
35 |
220 |
* Only one change to last API call.
This:
gi_tr => 'iterate',
becomes this:
gi_tr => ['iterate1', 'iterate2']
$tree->table2() : New API Call to Unroll a Table
After 2 or 3 years with "table()", I began to develop production
websites with it and decided it needed a cleaner interface, particularly
in the area of handling the fact that "id" tags will be the same after
cloning a table row.
First, I will give a dry listing of the function's argument parameters.
This will not be educational most likely. A better way to understand how
to use the function is to read through the incremental unrolling of the
function's interface given in conversational style after the dry
listing. But take your pick. It's the same information given in two
different ways.
Dry/technical parameter documentation
"$tree->table2(%param)" takes the following arguments:
* "table_ld => $look_down" : optional
How to find the "table" element in $tree. If $look_down is an
arrayref, then use "look_down". If it is a CODE ref, then call it,
passing it $tree.
Defaults to "['_tag' => 'table']" if not passed in.
* "table_data => $tabular_data" : required
The data to fill the table with. *Must* be passed in.
* "table_proc => $code_ref" : not implemented
A subroutine to do something to the table once it is found. Not
currently implemented. Not obviously necessary. Just created because
there is a "tr_proc" and "td_proc".
* "tr_ld => $look_down" : optional
Same as "table_ld" but for finding the table row elements. Please
note that the "tr_ld" is done on the table node that was found below
*instead* of the whole HTML tree. This makes sense. The "tr"s that
you want exist below the table that was just found.
* "tr_data => $code_ref" : optional
How to take the "table_data" and return a row. Defaults to:
sub { my ($self, $data) = @_;
shift(@{$data}) ;
}
* "tr_proc => $code_ref" : optional
Something to do to the table row we are about to add to the table we
are making. Defaults to a routine which makes the "id" attribute
unique:
sub {
my ($self, $tr, $tr_data, $row_count, $root_id) = @_;
$tr->attr(id => sprintf "%s_%d", $root_id, $row_count);
}
* "td_proc => $code_ref" : required
This coderef will take the row of data and operate on the "td" cells
that are children of the "tr". See "t/table2.t" for several usage
examples.
Conversational parameter documentation
The first thing you need is a table. So we need a look down for
that. If you don't give one, it defaults to
['_tag' => 'table']
What good is a table to display in without data to display?! So you
must supply a scalar representing your tabular data source. This
scalar might be an array reference, a "next"able iterator, a DBI
statement handle. Whatever it is, it can be iterated through to
build up rows of table data. These two required fields (the way to
find the table and the data to display in the table) are "table_ld"
and "table_data" respectively. A little more on "table_ld". If this
happens to be a CODE ref, then execution of the code ref is presumed
to return the "HTML::Element" representing the table in the HTML
tree.
Next, we get the row or rows which serve as sample "tr" elements by
doing a "look_down" from the "table_elem". While normally one sample
row is enough to unroll a table, consider when you have alternating
table rows. This API call would need one of each row so that it can
cycle through the sample rows as it loops through the data.
Alternatively, you could always just use one row and make the
necessary changes to the single "tr" row by mutating the element in
"tr_proc", discussed below. The default "tr_ld" is "['_tag' =>
'tr']" but you can overwrite it. Note well, if you overwrite it with
a subroutine, then it is expected that the subroutine will return
the "HTML::Element"(s) which are "tr" element(s). The reason a
subroutine might be preferred is in the case that the HTML designers
gave you 8 sample "tr" rows but only one prototype row is needed. So
you can write a subroutine, to splice out the 7 rows you don't need
and leave the one sample row remaining so that this API call can
clone it and supply it to the "tr_proc" and "td_proc" calls.
Now, as we move through the table rows with table data, we need to
do two different things on each table row:
* get one row of data from the "table_data" via "tr_data"
The default procedure assumes the "table_data" is an array
reference and shifts a row off of it:
sub { my ($self, $data) = @_;
shift(@{$data}) ;
}
Your function MUST return undef when there is no more rows to
lay out.
* take the "tr" element and mutate it via "tr_proc"
The default procedure simply makes the id of the table row
unique:
sub { my ($self, $tr, $tr_data, $row_count, $root_id) = @_;
$tr->attr(id => sprintf "%s_%d", $root_id, $row_count);
}
Now that we have our row of data, we call "td_proc" so that it can
take the data and the "td" cells in this "tr" and process them. This
function *must* be supplied.
Whither a Table with No Rows
Often when a table has no rows, we want to display a message
indicating this to the view. Use conditional processing to decide
what to display:
name | age | weight |
NATURE BOY RIC FLAIR |
35 |
220 |
SEE ALSO
* HTML::Tree
A perl package for creating and manipulating HTML trees
* HTML::ElementTable
An HTML::Tree - based module which allows for manipulation of
HTML trees using cartesian coordinations.
* HTML::Seamstress
An HTML::Tree - based module inspired by XMLC
(), allowing for dynamic HTML
generation via tree rewriting.
AUTHOR
Terrence Brannon,
COPYRIGHT AND LICENSE
Copyright (C) 2004 by Terrence Brannon
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself, either Perl
version 5.8.4 or, at your option, any later version of Perl 5
you may have available.
|