views:

171

answers:

6

HTML is a tree of nodes, before all. It's not just a text.

However, most templating engines handle their input and output as it was just a text; they don't care what happens around their tags, their {$foo}'s and <% bar() %>'s; also they don't care about what are they outputting. Sometimes they happen to produce a correct html, but that's just a coincidence; they didn't aim for that, all they wanted is to replace some funny marks in the text stream with their evaluation.

There are a few templating engines which do treat their output as a set of nodes; XSLT and Haml come to mind. For some tasks, this has advantages: for example, you can automatically reformat (like, delete all empty text nodes; auto-indent; word-wrap). The result is guaranteed to be a correct xml/sgml unless you use a strict subset of operations that can break that. Also, such templating engine would automatically quote strings, differently in text nodes and in attributes, because it strictly knows whether you're writing an attribute or a text node. Moreover, it can conditionally remove a node from output because it knows where it does begin and end, which is useful, and do other non-trivial node operations.

You might not like XSLT for its verbosiness or functionalness, but it's damn helps that your template is xmllint-able XML, and your output is a good sgml/xml.

So the question is: Which template engines do you know that treat their output as a set of correct nodes, not just an unstructured text? I know XSLT, Haml and some obscure python-based one. Moar!

+1  A: 

Basically all templating engines which use XML as their file format (for defining templates). By using XML, they enforce that the file must be well-formed.

[EDIT] Examples are: Genshi (Python) or JSP 2.0 (Java).

Aaron Digulla
Well, which ones do you specifically know?
alamar
+1  A: 

TAL (originally part of Zope but now implemented in a variety of languages) is XML-based. It's very logical and intention-revealing to work with - instead of shoving in a heap of text you're telling the template something like "set the href attribute of this link to http://google.com/ and set its text content to 'Search Google'. You don't have to manage which strings need to be escaped - generally if you intend something to be interpreted as markup, you put it in a template, and if you don't intend it to be interpreted as markup you feed it in as an attribute value or text content and TAL will escape it correctly.

d__
+1  A: 

With the Nagare web framework, the views are always a tree of XML nodes, directly built in Python. The tree can then be manipulated in Python, transformed with XSL, serialized in HTML or XHTML ...

(the 'nagare.namespaces' package comes with the Nagare projet but can be used in any Python application)

Example:

>>> from nagare.namespaces import xhtml_base
>>> h = xhtml_base.Renderer()    # The XHTML tree builder
>>>
>>> # Building the tree of nodes
>>> with h.html:
>>>    with h.body:
>>>        h << h.h1('Hello world')
>>> tree = h.root  # The tree root element
>>>
>>> print tree.write_xmlstring() # Tree serialized in XML

<html><body><h1>Hello world</h1></body></html>
+2  A: 

Which template engines do you know that treat their output as a set of correct nodes

Surprisingly ASP.NET does! You can change the HTML output of the page through a kind of DOM if you want: http://en.wikipedia.org/wiki/Asp.net#Rendering%5Ftechnique

Matthew Lock
+1  A: 

I maintain a list of push-style templating systems here: http://perlmonks.org/?node_id=674273

And am in the process of evaluating various Perl templating systems for their separation index: http://bit.ly/bXaYt7

But the tree-based one is written by me, HTML::Seamstress - http://search.cpan.org/dist/HTML-Seamstress/

The term "push-style" comes from Terence Parr's Paper "Enforcing Strict Model-View Separation in Template Engines" - http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf

metaperl
A: 

Also, http://snapframework.com/docs/tutorials/heist from Haskell's snap seems to fit.

alamar