[stringtemplate-interest] Maps vs aggregates vs beans

Robert Rowland robertdavidrowland at gmail.com
Thu Jul 27 00:58:06 PDT 2006


Sorry I sent this too early . . . the example at the bottom is now complete.


If you only have one hashmap and your template was as follows it would
work fine.

StringTemplate t = new StringTemplate("<b>$name.a$ - $name.b$</b>");

So something is behaveing differently depending depending on whether
you a single hashmap or multiple hashmaps set for a given attribute.
I'm guessing this is intended behaviour but others will know the
answer to that better than me.

In situations where you may have 1 or more hashmaps then this is the
work around I've used.

StringTemplate t = new StringTemplate("$name:{n|$if(n)$<b>$it.a$ -
$it.b$</b>$endif$}$");

Then after I've set my 1 or more hashmaps, I go and set an empty one
as well, which is ignored because of the $if(n)$

while(myloopmaybe_0_1_or_many_items) {
    Hashmap name = new Hashmap();
    name.put("a", "first value");
    name.put("b", "second value");
    template.setAttribute("name", name);
}

Hashmap emptyName = new Hashmap();
template.setAttribute("name", emptyName);

I hope this helps.  I'll be interested to know if the behaviour that
requires this work around is a bug or is intended behaviour.

> On 7/27/06, Steven Byrnes <erewhon at flatland.org> wrote:
> > Hi,
> >
> > I'm running into a problem with aggregates and multi-valued attributes.  Not sure if it's lack of
> > understanding on my part or something else... I'm running String Template 2.3b7.
> >
> > For the following template:
> >
> >  StringTemplate t = new StringTemplate("$name:{ <b>$it.a$ - $it.b$</b> }$");
> >
> > if I use 1 or more aggregate attribute calls, it works, e.g.:
> >
> >  t.setAttribute("name.{a,b}", "foo", "blah");
> >  t.setAttribute("name.{a,b}", "bar", "brap");
> >
> > same thing for a simple bean.  One or more setAttributes() will work.
> >
> >  t.setAttribute("name", new data("foo", "bar"));  // where data() has getA() and getB() methods
> >
> > If I use setAttribute() for multiple Hashmaps, it works.  But if I only use one, I get an error, e.g.:
> >
> >  HashMap<String,String> m = new HashMap<String,String>();
> >  m.put("a", "foo"); m.put("b", "blah");
> >  t.setAttribute("name", m);
> >  System.out.println(t.toString());
> >
> >  =>
> >
> >  Class java.lang.String has no such attribute: a in template context [anonymous anonymous]
> >  java.lang.NoSuchFieldException: a
> >        at java.lang.Class.getField(Class.java:1507)
> >        at org.antlr.stringtemplate.language.ASTExpr.rawGetObjectProperty(ASTExpr.java:410)
> >        at org.antlr.stringtemplate.language.ASTExpr.getObjectProperty(ASTExpr.java:314)
> >  ....
> >
> > Any ideas?
> >
> > Thanks,
> > Steven
> > _______________________________________________
> > stringtemplate-interest mailing list
> > stringtemplate-interest at antlr.org
> > http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
> >
>


More information about the stringtemplate-interest mailing list