Early table rendering in Seamstress was heavily influenced by Haskell and was rather non-perlish. Recent industry demands to render tables quickly and easily have led to new developments in HTML::Element::Library and other related modules.
The strongest influence on modern HTML::Seamstress development was a PHP AJAX library called KOOLPHP.
KOOL PHP allowed HTML development to be data-oriented rather than HTML-oriented.
In this post, multiple tables in a single HTML file are rendered using HTML::Element::Replacer, a Perl library developed after being inspired by KOOLPHP.
Our goal is to populate the HTML tables...<table> <tr seam_class="beer_row"> <td kmap="brand"> blah </td> <td kmap="age"> blah </td> </tr> </table> <table> <tr seam_class="fruit_row"> <td kmap="fruit"> blah </td> <td kmap="color"> blah </td> </tr> </table>
Ok, that is our code for rendering tables... In the second table, we do alternating table rows courtesy of List::Cycle.use HTML::Element::Replacer; my @beer = ( { brand => 'miller', age => 12 }, { brand => 'coors' , age => 15 }, { brand => 'coke' , age => 22 } ); my @fruit = ( { fruit => 'apple' , color => 'red' }, { fruit => 'orange' , color => 'orange' }, { fruit => 'banana' , color => 'yellow' } ); # table_example is a Perl package that creates an # HTML::TreeBuilder instance of the file table_example.html # actually it creates an HTML::Seamstress instance, but # HTML::Seasmtress @ISA HTML::Tree use table_example; my $tree = table_example->new; { my $replacer = HTML::Element::Replacer->new (tree => $tree, look_down => [ seam_class => 'beer_row' ]); for my $data (@beer) { # defmap is in HTML::Element::Library $replacer->push_clone->defmap(kmap => $data); } } # BRACES ARE NECESSARY for HTML::Element::Replacer to work! { use List::Cycle; my $row_style = List::Cycle->new( { values => [ qw/main alt/ ] } + ) ; my $replacer = HTML::Element::Replacer->new (tree => $tree, look_down => [ seam_class => 'fruit_row' ]); for my $data (@fruit) { my $elem = $replacer->push_clone; $elem->defmap(kmap => $data); # clone and push onto @temp_list $elem->attr(row_style => $row_style->next); } } warn $tree->as_HTML(undef, ' ');
HTML::Seamstress has a script called spkg.pl that creates a perl class that gives you the HTML file in tree form as an instance... so let's give ourselves a Perl class for the file table_example.html
Notice that the HTML files can be in one directory tree (html_file_path) while the root path for the perl packages which create HTML::Tree instances of these HTML files can be in another (comp_root).[tbrannon@devel tables]$ spkg.pl --base_pkg=Base file.html comp_root........ /home/tbrannon/prg/html-element-replacer/HTML-Elemen +t-Replacer/ex/tables/ html_file_path... /home/tbrannon/prg/html-element-replacer/HTML-Elemen +t-Replacer/ex/tables/ html_file........ file.html html_file sans... file file.html compiled to package file
A very simple class which tells spkg.pl where the root of our HTML files is. It subclasses HTML::Seamstress so that its various constructors are available as well various OO tree processing libraries. HTML::Element::Library is the current kitchen main OO tree processing library, but an number can be added with object-oriented ease.package Base; use base qw(HTML::Seamstress) ; sub comp_root { '/home/tbrannon/prg/html-element-replacer/HTML-Element +-Replacer/ex/tables' } 1;
In reply to Easy table rendering in modern HTML::Seamstress by metaperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |