the following tree results:<html> <head> <title>Doc 1</title> </head> <body> Stuff <hr> 2000-08-17 </body> </html>
This slide provides another example of representing an HTML document as a tree.html / \ head body / / | \ title "Stuff" hr "2000-08-17" | "Doc 1"
The popular Perl HTML templating systems do not treat HTML manipulation as tree manipulation. At least not directly, because it may be the case that all programs and data structures can be represented as a tree (correct me on this). The popular Perl systems treat HTML as a character string and provide simple pseudo-operators to manipulate the display logic of this string.
While this is intuitive for programmers and designers alike, it is instructive to look at radically different approaches. In this article, I move through a number of common pseudo operators and HTML manipulations and show how each of these can be interpreted as a tree rewriting operation. Because Template is so well-documented and provides a representative feature set, it is easy to use for this purpose.
In the template HTML, we start with three candidate nodes:[% IF age < 10 %] Hello, does your mother know you're using her AOL account? [% ELSIF age < 18 %] Sorry, you're not old enough to enter (and too dumb to lie about your age) [% ELSE %] Welcome [% END %]
And based on the conditional, we delete or preserve the child nodes. Now, I have looked at number of practical solutions for implementing this tree op in Perl, and after looking atROOT child1: Hello, does your mother know you're ... child2: Sorry, you're not old enough to enter ... child3: Welcome
So, here is how we handle this task using HTML tree rewrites. First we markup the HTML so we can find it:
And now we process it using HTML::Tree,<span id=age_handler> <span id="under10"> Hello, does your mother know you're using her AOL account? </span> <span id="under18"> Sorry, you're not old enough to enter (and too dumb to lie about your age) </span> <span id="welcome"> Welcome </span> </span>
Hmm, I'm worn out. Let's make this the first installation in the ongoing saga entitled: how to do HTML templating via tree rewrites: the HTML::Seamstress approach.use HTML::TreeBuilder; my $tree = HTML::TreeBuilder->new(); $tree->parse_file($filename); $tree->age_handler($age); print $tree->as_HTML; sub age_handler { my ($tree, $age) = @_; my $SPAN = $tree->look_down('id', 'age_handler'); if ($age < 10) { $SPAN->look_down('id', $_)->detach for qw(under18 welcome); } elsif ($age < 18) { $SPAN->look_down('id', $_)->detach for qw(under10 welcome); } else { $SPAN->look_down('id', $_)->detach for qw(under10 under18); } }
Just one more comment: all of that
should definitely be abstracted into some HTML::Stitchery such as:look_down->detach() for ($this, $that)
$SPAN->KILL_CHILDREN (@children); # fodder for carnivore :)
Petal is Perl's implementation of ZOPE's TAL This framework does quite a bit --- too much for me to want to figure out. And at times I felt like I was using Text::MagicTemplate because I had to know quite a bit about what to do on the HTML side to get my Perl data to enter the XML properly. All I want to do on the HTML side is put little id attributes in the HTML, find' em and rewrite 'em.
Xelig is also inspired by XMLC, but it is quite different from Seamstress. It is interesting but not so well-documented at the moment.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTML Templating as Tree Rewriting: Part I: "If Statements"
by pg (Canon) on Oct 28, 2003 at 03:30 UTC | |
by theorbtwo (Prior) on Oct 28, 2003 at 13:03 UTC | |
by bart (Canon) on Oct 28, 2003 at 22:47 UTC | |
|
Re: HTML Templating as Tree Rewriting: Part I: "If Statements"
by dakkar (Hermit) on Oct 28, 2003 at 19:06 UTC | |
|
Re: HTML Templating as Tree Rewriting: Part I: "If Statements"
by cbraga (Pilgrim) on Oct 28, 2003 at 17:46 UTC | |
by princepawn (Parson) on Oct 28, 2003 at 18:58 UTC |