[stringtemplate-interest] Does StringTemplate support components?

Nate misc at n4te.com
Wed Dec 27 08:23:17 PST 2006


You can subclass StringTemplateGroup and override 
loadTemplateFromBeneathRootDirOrCLASSPATH to intercept template lookups. 
You could switch on the name and configure different templates a certain 
way. This way the templates are configured by Java code even if included 
through other templates. I think this is what you were getting at. I'm 
not sure this is better than the monolithic approach, but it is possible.

-Nate


Adam Bennett wrote:
> I really appreciate all the suggestions but I haven't heard the answer 
> yet.  I'll try to reformulate my the question:
>  
> Question 1) From my reading, it appears that StringTempate supports 
> calling other templates as if it were a subroutine, right?  I infer 
> this from the "Expressions" document where it is talking about 
> "Template References":
>  
> "the enclosing page template would automatically create an instance of 
> the referenced template and insert it"
>  
> What I am focusing on here is "automatically create an instance".  
> Beautiful.  Less coding.
>  
> Question 2) The documentation goes on to describe 2 ways of getting 
> the necessary attributes to the subroutine template.
> 1) "Accessing Attributes Of Enclosing Template"
> 2) "Passing Parameters To Another Template"
>  
> Are there any other ways?  You see, what I am looking for is a way to 
> help the web designers work independently of the programmers.  Here's 
> a scenario to describe what I mean.  Let's say that our web 
> application handles the URL "/OurWebApp/account/summary.page" with 
> Java code like this:
>  
> StringTemplate st = group.GetInstanceOf("account/summary.st");
> ... do some database queries
> st.setAttribute("foo", queryResult1);
> st.setAttribute("bar", queryResult2);
> ... render the template and send it to the client browser
>  
> The above code is maintained by the programmers.  The web designers 
> can tweak the template text to their hearts content.  Initially it 
> looks like:
> <html> 
> <body>
> ...
> $foo$
> $bar$
> ...
> </body>
> </html> 
>  
> But wait!  The web designer is changes her [artistic] mind and decides 
> to take the summary page the next level.  She wants to show the users 
> account balance so she makes a call to the accountBalanceBox template.
> <html> 
> <body>
> ...
> $foo$
> $bar$
> $accountBalanceBox()$
> ...
> </body>
> </html> 
>  
> But accountBalanceBox needs a $dollars$ attribute.  This can only be 
> determined by getting the user ID from the HttpSession and then 
> querying the database for it.  But that's the job of the programmer.  
> If we have support for true componentization then the Java code that 
> handles summary.page should NOT need to set the $dollars$ attribute.  
> The general principle here is separation of concerns 
> (http://en.wikipedia.org/wiki/Separation_of_concerns).  If 
> accountBalanceBox is made to be a logical component, which includes 
> both a template as well as some Java code, it gives web designers full 
> freedom to mix and match the components that have been provided by the 
> programmers.
>  
> So I think the mechanism I am after is some sort of hook.  As 
> StringTemplate encounters accountBalanceBox it calls my Java function 
> which gives me a chance to provide the necessary attributes.  Such a 
> hook might look like this:
>  
> void templatePreprocess(StringTemplate st, StringTemplateContext context)
> {
>   if (st.getName().equals("accountBalanceBox"))
>   {
>     HttpSession session = context.getAttribute("session");
>     int userID = session.getAttirbute("userID");
>     st.setAttribute("dollars", database.getBalance(userID));
>     return;
>   }
>   ...
> }
>  
> Does such a mechanism exist?
>  
> Thanks much.
> - Adam
>  
>  
> ------------------------------------------------------------------------
>
> _______________________________________________
> 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