##
name age weight
NATURE BOY RIC FLAIR
35
220
####
use HTML::Seamstress;
# load the view
my $seamstress = HTML::Seamstress->new_from_file('simple.html');
# load the model
my $o = Simple::Class->new;
my $data = $o->load_data;
# find the and
my $table_node = $seamstress->look_down('id', 'load_data');
my $iter_node = $table_node->look_down('id', 'iterate');
my $table_parent = $table_node->parent;
# drop the sample and from the HTML
# only add them in if there is data in the model
# this is achieved via the $add_table flag
$table_node->detach;
$iter_node->detach;
my $add_table;
# Get a row of model data
while (my $row = shift @$data) {
++$add_table;
# clone the sample
my $new_iter_node = $iter_node->clone;
# find the tags labeled name age and weight and
# set their content to the row data
$new_iter_node->content_handler($_ => $row->{$_})
for qw(name age weight);
# push this table row onto the table
$table_node->push_content($new_iter_node);
}
# reattach the table to the HTML tree if we loaded data into some table rows
$table_parent->push_content($table_node) if $add_table;
print $seamstress->as_HTML;
####
use Inline::Java; # grin ;-)
Shape[] shapes = new Shape[100];
shapes[0] = new Rectange(45,27);
shapes[1] = new Circle(10);
shapes[2] = new Ellipse(25,13);
shapes[3] = new Quadrilateral(15,20,50,14);
...
double area = 0;
for (int i=0; i < shapes.length; i++)
area += shapes[i].computeArea();
####
name age weight
NATURE BOY RIC FLAIR
35
220
####
use HTML::Seamstress;
# load the view
my $seamstress = HTML::Seamstress->new_from_file('simple.html');
# load the model
my $o = Simple::Class->new;
$seamstress->table
(
# tell seamstress where to find , via the method call
# ->look_down('id', $gi_table). Seamstress detaches the table from the
# HTML tree automatically if no table rows can be built
# "gi" stands for generic identifier. Most people call "gi"s tags, but
# mirod calls them "gi"s so I will too :)
gi_table => 'load_data',
# tell seamstress where to find the .
# this is the major place where DWIM will come in!
gi_tr => 'iterate',
# the model data to be pushed into the table, row by row
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
# content_handler() is a Seamstress function which takes
# $id_val and $content as args. It does a ->look_down('id', $id_val)
# and sets the content section of the found node (a ) to $content
td_data => sub { my ($tr_node, $tr_data) = @_;
$tr_node->content_handler($_ => $tr_data->{$_})
for qw(name age weight) }
);
print $seamstress->as_HTML;
####
name age weight
#### ONE BGCOLOR
NATURE BOY RIC FLAIR
35
220
### ANOTHER BGCOLOR
NATURE BOY RIC FLAIR
35
220
####
gi_tr => 'iterate',
####
gi_tr => [qw(iterate1 iterate2)],
####
sub table {
my ($s, %table) = @_;
my @table_gi_tr = listify $table{gi_tr} ;
...
####
my @table_gi_tr = (ref $table{gi_tr} eq 'ARRAY')
? (@$table{gi_tr})
: ( $table{gi_tr})
####
# fold the scalar and aref into an array
my @table_gi_tr = listify $table{gi_tr} ;
# create an array containing HTML::Element pointers to the or s
my @iter_node = map
{
$table->{table_node}->look_down($ID, $_)
} @table_gi_tr;
# tie a scalar to the list of so that we can cycle thru 'em
tie $table->{iter_node}, 'Tie::Cycle', \@iter_node;
# iterate through the model data
while (my $row = $table{tr_data}->($table, $table{table_data}))
{
# pick out a to display the row of model data with:
my $I = $table->{iter_node}; # force the tied data to FETCH into $I
my $new_iter_node = $I->clone; # clone the dummy for templating
# wont work: my $new_iter_node = $table->{iter_node}->clone;
...
}
####
our ($table_data, $tr_data, $gi_td);
sub table {
my ($s, %table) = @_;
my $table = {};
$table->{table_node} = $s->look_down($ID, $table{gi_table});
my @table_gi_tr = listify $table{gi_tr} ;
my @iter_node = map
{
$table->{table_node}->look_down($ID, $_)
} @table_gi_tr;
tie $table->{iter_node}, 'Tie::Cycle', \@iter_node;
$table->{content} = $table{content};
$table->{parent} = $table->{table_node}->parent;
$table->{table_node}->detach;
$_->detach for @iter_node;
my $add_table;
while (my $row = $table{tr_data}->($table, $table{table_data}))
{
++$add_table;
# wont work: my $new_iter_node = $table->{iter_node}->clone;
my $I = $table->{iter_node};
my $new_iter_node = $I->clone;
$table{td_data}->($new_iter_node, $row);
$table->{table_node}->push_content($new_iter_node);
}
$table->{parent}->push_content($table->{table_node}) if $add_table;
}