[stringtemplate-interest] addAttribute/addAttributes weirdness

Brian Lewis bsl04 at uark.edu
Sat Mar 24 20:34:09 PDT 2007


I'm writing StringTemplate 3.0 group files and noticing some strange
differences between addAttribute(String, String) and addAttributes(Map).

group test;
page(title, cssFile) ::=
"<html>$\n$$title$$\n$$if(cssFile)$$cssFile$$endif$$\n$</html>"
somePage() ::= "$page(title={title of the page})$"

If I getInstanceOf("somePage") and setAttribute("cssFile", "style.css"),
I get NoSuchElementException because cssFile isn't in somePage's
argument list, even though it *is* in page's argument list.

If I put cssFile in somePage's argument list and take it out of page's,
I get the desired effect, but the usefulness of page is decreased
because the templates being wrapped are having to care about cssFile,
which should only be the concern of page.

http://www.antlr.org/wiki/display/ST/Group+Files says that "If you
reference an attribute that is not formally defined in that template
or any enclosing template, you also get [NoSuchElementException]." In
violation (I think) of that, I can put something like $nonesuch$
anywhere in one of my group file templates like somePage and I don't
get an exception.

If I put "cssFile" -> "style.css" in a Map and addAttributes() it to
somePage, I don't get NoSuchElementException, even though cssFile isn't
in somePage's argument list. That seems inconsistent.

If I remove cssFile from page's argument list like so:
group test;
page(title) ::=
"<html>$\n$$title$$\n$$if(cssFile)$$cssFile$$endif$$\n$</html>"
somePage() ::= "$page(title={title of the page})$"

and addAttributes(), I get the obvious desired output. But why was it
allowed? cssFile isn't in page's argument list. Why was having cssFile
in page's argument list suppressing $cssFile$?

We're trying to make a StringTemplateView for Spring. The natural way
to set the attributes is the setAttributes() Map way. The problem is
that using setAttributes() seems to disable error checking (?) and
requires that you counterintuitively remove things from the template
argument lists.

I hope someone will agree that this stuff is pretty weird. Any help
would be greatly appreciated. Thanks.


More information about the stringtemplate-interest mailing list