[antlr-interest] String Template iteration

Barnes, Jeff JB0284 at att.com
Tue Jul 24 09:35:06 PDT 2007


I've been trying new things (to me) with StringTemplate and made a
couple of observations.

The separation of concerns is very clear. It makes it easy to think in
an MVC manner. I've done successful "versions" of UI's using html and
xml.
The compiled code doesn't change... only the template/group. Very nice!

One of the things I was hoping to avoid was iteration code for, say, a
combo box.

for (int i = 0; i < options.length; i++) {
  StringTemplate option = group.getInstanceOf("option");
  option.setAttribute("value", myval[i]);
  option.setAttribute("text", mytext[i]);
  option.setAttribute("selected", ( myval[i].equals(selectedVal) )? 
    "selected=\"selected\"" : "");
}

The piping feature in StringTemplate allows me to let StringTemplate
handle some iteration. I abstract a "form" into an object with accessors
and pass it to StringTemplate. Very nice!!! But that "selected" problem
represents a state of the ui, so I don't know how to pass that except as
a Boolean array. So that

<select>
$myForm.options, selectedIndices:{option, isSelected | <option
value="$option.value$"
$if(isSelected)$selected="selected"$endif$>$option.text$</option>};separ
ator="\n"$
</select>

I feel an objection rising in me about creating a Boolean array just for
the selected state. The objection is along the lines of "that's a lot of
work!" I answer myself with, "but it represents a very clear separation
of model/view concerns". I was wondering if there was another way to
look at the problem that provides a solution using StringTemplate
without iteration.

Best regards,
Jeff



More information about the antlr-interest mailing list