| Next | Los Angeles Perl Mongers | 6 |
HTML
<html>
<table id="load_data">
<tr> <th>name</th><th>age</th><th>weight</th> </tr>
<tr id="iterate">
<td id="name"> NATURE BOY RIC FLAIR </td>
<td id="age"> 35 </td>
<td id="weight"> 220 </td>
</tr>
</table>
</html>
Perl
require 'simple-class.pl';
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 <table> and <tr>
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 <table> and <tr> 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) {
# We got row data. Set the flag indicating ok to hook the table into the HTML
++$add_table;
# clone the sample <tr>
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);
$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;
| More Details | ![]() |
November 2003 |