[stringtemplate-interest] Template Group Organization

Terence Parr parrt at cs.usfca.edu
Mon Dec 8 11:01:27 PST 2008


On Dec 5, 2008, at 6:09 AM, Nathan Ward wrote:

> Are there any typical usage patterns or best practices for "template  
> group organization?" I haven't seen much said about this.
>
> I believe the options supported by the StringTemplate API is to  
> either treat a set of directories as a template group in which case  
> each file within that directory or in a subdirectory as a template.  
> Template Group Files are not allowed in this case. The other option  
> is to treat a single .stg file as a template group. In this case, a  
> file can contain multiple templates.

When I rebuild ST, I'm going to fix this artifact of how ST grew.  
Directories full of templates and .stg files will be uniform. A group  
file will look like a directory of templates.

> I'm working on generating files for a Java web application including  
> JSPs, Struts Action classes, form classes, resource bundle files,  
> etc. What I've done so far is to use Template Group Files with a  
> template group file for each target file, e.g. JSP.stg,  
> ActionClass.stg, ApplicationResourcesProperties.stg. Each of these  
> template groups have a number of templates specific to a particular  
> target output file. However, there are several small templates that  
> each of these template groups need to share. What I've done so far  
> is to make a Common.stg and use the StringTemplate Java API to  
> configure the output file specific template groups inherit from this  
> template group, e.g. the JSP string template group inherits from  
> Common string template group.
>
> I'm wondering if there is a better way to share templates between  
> template groups, but still be able to use template group files so  
> that I can group closely related templates into a file.

My guess is that you could use a group loader to look in one directory  
or group first and then another, simulating inheritance.

> Inheritance doesn't seem to be the best approach to this issue, but  
> currently it looks like it is the only choice. This also reduces the  
> clarity of the templates in some cases. For example, if a template  
> named resetButtonAction(button) refers to a template named  
> msgKeyTag(msgKey=button.name) where the msgKeyTag template is  
> inherited from Common.stg it isn't clear in the code that this  
> template is not defined in JSP.stg. A developer

Yes, but isn't that true in programming languages with classes? If you  
call foo(), you know that it might be from a superclass.

> I'd love to hear how other people organize templates into template  
> groups and how you organize the template group files. As it stands,  
> it seems to me that StringTemplate should allow nested template  
> groups so that I could specify a top-level directory as a template  
> group and then use template group files in subdirectories under that  
> base directory. I'd also like to be able to refer to a template in a  
> different template group with a syntax like

Yep, I would like to make this more consistent.

THanks for the thought and good ideas.
Ter


More information about the stringtemplate-interest mailing list