[stringtemplate-interest] Template Group Organization

Nathan Ward nward at resqsoft.com
Fri Dec 5 06:09:05 PST 2008


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. 
 
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. 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 working on these templates in the future
will have to search JSP.stg to find that it is not defined in that file,
then figure out that the JSP template group inherits from the Common
template group and search it. If my template group inheritance grows (which
it probably will due to the way that I'm using it), then the developer will
have to repeat this process or just search all .stg files in a given
directory. Seems kind of clunky. 
 
Part of the problem is that I'm setting up the template group inheritance
via the Java API rather than using the syntax at the top of a template group
file (e.g. group JSP : Common). However, I would like to separate my
templates into separate directories by various criteria mainly so that I can
find and replace all templates specific to a certain technology (Struts,
Hibernate, etc.). So, I have the Common.stg template in a different
directory from JSP.stg. The string template group syntax doesn't support
specifying a path in the super group name. 
 
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
$Shared/OtherTemplateGroup::otherTemplate(...)$ where "Shared" is a
directory relative to some base directory and therefore could be thought of
as another template group and OtherTemplateGroup a template group file in
the Shared directory. And, I would like to be able to specify a relative
path when specifying the super template group in a template group file, e.g.
JSP : Shared/BaseJsp.stg where Shared is relative to the base directory.
 
Nathan Ward
ResQSoft, Inc.
703.861.9103
www.resqsoft.com
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20081205/77de000d/attachment.html 


More information about the stringtemplate-interest mailing list