Paul Seamons wrote:
The Velocity syntax has an EVALUATE directive
I've got no particular objection to it. However, I'm not sure I see any real
benefit to it over an eval filter/vmethod, other than for the sake of
completeness in saying we've got it.
# TT2
[% text | eval %]
# TT3
[% text.eval %]
[% "Hello [% world %]".eval %]
> Bonus points for supporting configuration arguments to
> be passed along.
[% text.eval(ignore="/* */", interpolate=1) %]
The other related thing I've got planned for TT3 is a template() vmethod which
turns a string into a reusable template:
[% message = "Hello [% item or 'World' %]!".template %]
[% message %] # Hello World!
[% message(item="Badger") %] # Hello Badger!
That would be another place to put any configuration items.
[% message = text.template(interpolate=1, tags='<* *>') %]
By the magic of supporting different template types (which is a subject I'm
about to come to in another reply as soon as I've got this one out of the
way), you should also be able to specify a template type to select a
pre-defined template style:
[% message = text.template(type='tt2') %]
[% message = text.template(type='tt3') %]
[% message = text.template(type='velocity') %]
Furthermore, you should be able to bolt on vmethods that handle the
template-ification to different template languages:
[% message = text.tt2 %]
[% message = text.tt3 %]
[% message = text.tt3(tags='<* *>') %] # with options
I'm thinking this general approach would also work quite nicely for other
things, like turning a chunk of XML into a DOM, for example:
[% # some example XML
article = "<article>..blah blah...</article>";
# xml() vmethod parses into DOM
dom = article.xml;
# now do whatever you like with it DOM-wise
FOREACH article IN dom.getElementsBlahBlah(..etc..)
%]
Or even:
[% products_under = "SELECT * FROM products WHERE price < ?".sql %]
[% FOREACH product IN products_under(12.99);
...etc...
END
%]
OK, I know it's generally a bad idea to embed SQL in your templates, but it
illustrates the principal.
Incidentally, there's nothing to stop anyone from writing xml() or sql()
vmethods for TT2 right now, although you can't call vmethods on literal
strings so you have to do it like the XML example rather than the SQL one.
However, getting the template() and related vmethods to work in TT2 is a
rather more complicated matter. I think I've got it cracked in TT3, but
that's for another post...
Cheers
A
PS Did I get the bonus points? :-)
_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates