[stringtemplate-interest] setAttribute() method chaining

Nathan Ward nward at resqsoft.com
Fri Jan 23 10:39:53 PST 2009


The method chaining is clearer, simpler,  and more convenient which matters
when I'm trying to get buy-in from other developers in our organization. So,
the ability to do this would be appreciated if it could be worked in at some
point.

 

   Regards,

 

   Nathan

 

From: Terence Parr [mailto:parrt at cs.usfca.edu] 
Sent: Friday, January 23, 2009 1:09 AM
To: Nathan Ward
Cc: stringtemplate-interest at antlr.org
Subject: Re: [stringtemplate-interest] setAttribute() method chaining

 

Hi. a good idea.  Though, I think it's easier to just do

 

jspTemplate.setAttributes(aMap);

 

and set aMap appropriately using Java stuff.  Does HashMap allow setter
chaining?  If not, you can do:

 

aMap = new HashMap() {

            {

            put("templateContext", this.templateContext);

      put("project", this.project);

      ...

};

 

On Jan 22, 2009, at 2:51 PM, Nathan Ward wrote:





Terence,

 

I have been integrating StringTemplate with a home grown JSP based template
solution that we have used for several years. It would be convenient in this
case to use "method chaining" if the setAttribute() method returned the
instance of the StringTemplate.

 

e.g.

Instead of:

                StringTemplate jspTemplate =
jspTemplateGroup.getInstanceOf("jsp");

                jspTemplate.setAttribute("templateContext",
this.templateContext);

                jspTemplate.setAttribute("project", this.project);

                jspTemplate.setAttribute("screen", screen);

               String content = jspTemplate.toString();

 

I could do:

String content = jspTemplateGroup.getInstanceOf("jsp")

.setAttribute("templateContext", this.templateContext)

                .setAttribute("project", this.project)

                .setAttribute("screen", screen)

               .toString();

 

This is particularly convenient in cases where I have a block of code that
needs to use more than one StringTemplate template instance. It mainly saves
me from having to think up separate names for the StringTemplate variables
and also seems to be more readable than a set of separate statements since
there is less non-value added text. All it would take is the setAttribute()
method to return the current StringTemplate object (i.e. "return this;").
For now, I've written a simple wrapper for StringTemplateGroup and
StringTemplate classes and had to create a method named "newTemplate()" that
mimics "getInstanceOf()" and a "setParam()" method that just calls
setAttribute() but also returns itself.

 

   Nathan

 

Nathan Ward

ResQSoft, Inc.

www.resqsoft.com

 

_______________________________________________
stringtemplate-interest mailing list
stringtemplate-interest at antlr.org
http://www.antlr.org/mailman/listinfo/stringtemplate-interest

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20090123/16ecbbec/attachment-0001.html 


More information about the stringtemplate-interest mailing list