[whatwg] template element?
hsivonen at iki.fi
Sat Aug 28 11:17:34 PDT 2004
On Aug 4, 2004, at 11:58, Wrigley, Ave wrote:
>>> Without opening too much of a can of worms ... are there any lessons
>>> to be learned / wheels left un-re-invented?
>> For one thing, designing a terse templating language where
>> the template directives are XML elements is hard. The supposedly terse
>> becomes verbose really easily in my experience. See
> Why do they have to be XML elements?
The Web Forms spec presupposes that templating is done declaratively by
mixing some templating syntax with the target language. (Note that this
is not the only possible way to do templating. Another way with
considerable merit is that the template does not contain special syntax
beyond the target language but contains some hooks such as id
attributes that a mutator program access.)
Given that presupposition there are 3 options (please let me know if I
have missed something):
1) Reg exp substitution
The template is loaded into a buffer and a sequence of regular
expression substitution is performed upon it.
2) Target as literal
The template language has a proper context-free grammar so that a
syntax tree could be constructed. Snippets of the target language that
are output verbatim are treated as string literals in the grammar of
the template language.
3) Combined tree
The syntax tree for templating also contains nodes for representing the
syntax of the target language. Put the other way, the syntax tree of
the target language is augmented with nodes for templating.
With the options #1 and #2 the processing of the template language
yields a string which is then fed to a parser that parses the target
language. With option #3 the processing of the template language yields
a syntax tree for the target language without reparsing.
Since Web Forms 2 is intended for use in interactive applications,
returning to the templating phase in response to an user event and
reparsing is not an attractive proposition and pretty much rules out
options #1 and #2. Also, when well-formed output is required, option #3
is clearly superior in helping to guarantee correct outcome. Therefore,
we're left with option #3.
In the context of XML and HTML, the document tree in can be used as
"the syntax tree of the output language". Now the question is should
new templating nodes be introduced or can the existing node types be
reused. Adding new node types is expensive, because that would preclude
the use of existing document tree implementations.
When the existing node types are reused, using the element nodes for
templating is the obvious choice, because element nodes can have child
One thing that has come up in the context of WHAT WG but that I failed
to realize when I was designing a templating language is that you can
use attributes to add the template nature to elements of the target
language and reduce the overall number of elements.
hsivonen at iki.fi
More information about the whatwg