HTML::Seamstress is a fully object-oriented, completely non-invasive DOM-style (push-style) templating solution for Perl.

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.

first the html "template"

<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>
Our goal is to populate the HTML tables...

our Perl program

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, ' ');
Ok, that is our code for rendering tables... In the second table, we do alternating table rows courtesy of List::Cycle.

code and html are separated

because html is in one file and the code to process it is in another, we have infinite possibilities for dynamic composition of the two!!!

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

[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
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).

ok what is "Base"

Base is the base class for all your perl classes which will serve to turn HTML files into trees, lets take a look:
package Base; use base qw(HTML::Seamstress) ; sub comp_root { '/home/tbrannon/prg/html-element-replacer/HTML-Element +-Replacer/ex/tables' } 1;
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.

In reply to Easy table rendering in modern HTML::Seamstress by metaperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.