[stringtemplate-interest] setAttribute() method chaining

Nathan Ward nward at resqsoft.com
Thu Jan 22 14:51:47 PST 2009


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

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20090122/141fb472/attachment.html 


More information about the stringtemplate-interest mailing list