[stringtemplate-interest] Does StringTemplate support components?

Adam Bennett adamb at videx.com
Fri Dec 22 23:49:06 PST 2006


 
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  
   
       
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061222/cf166620/attachment.html 


More information about the stringtemplate-interest mailing list