Just another Perl shrine | |
PerlMonks |
HTML Templating as Tree Rewriting: Part I: "If Statements"by princepawn (Parson) |
on Oct 28, 2003 at 02:19 UTC ( [id://302606]=perlmeditation: print w/replies, xml ) | Need Help?? |
Until Sean Burke's articles, it never really occurred to me that HTML
could be represented and understood as a tree.
For example, given this HTML:
the following tree results: This slide provides another example of representing an HTML document as a tree. 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.
ifThe iftag of the pseudo language decides whether a node of the tree will remain or not:In the template HTML, we start with three candidate nodes: 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 at
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, 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. Just one more comment: all of that should definitely be abstracted into some HTML::Stitchery such as:
ResourcesThere are other systems on CPAN which are tree-oriented. My system, HTML::Seamstress grew out of Paul Lucas' HTML_Tree by way of Evoscript, all of which was inspired by the Java XMLC framework. XMLC compiles a webpage into a java tree with API hooks for the various tags in the HTML. After you do tree rewriting on the little XML objects in the java tree, the build method builds the HTML page.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.
Back to
Meditations
|
|