Below is the solution I came up with. It is nice that the internals of
StringTemplateGroup are exposed as protected members so that this is
even possible. However, I feel this is something StringTemplate should
provide out of the box. Unfortunately, simply including the class below
may prove confusing since the StringTemplateGroup class also provides a
way to load templates through the classpath. A simple "@see
ClasspathTemplateGroup" javadoc tag in the StringTemplateGroup class may
be sufficient to clear this up.
-Nate
public class ClasspathTemplateGroup extends StringTemplateGroup {
private final String classpathRootDir;
public ClasspathTemplateGroup (String name, String classpathRootDir) {
super(name);
if (!classpathRootDir.endsWith("/")) classpathRootDir += "/";
this.classpathRootDir = classpathRootDir;
}
protected StringTemplate loadTemplateFromBeneathRootDirOrCLASSPATH
(String fileName) {
return
super.loadTemplateFromBeneathRootDirOrCLASSPATH(classpathRootDir +
fileName);
}
}
Nate wrote:
> Hi Harry, please see my list message in reply to Jean-Marie (sent
> 11/12/06). I realize that I can access templates on the classpath using
> a path relative to the root of the classpath. However, I want to access
> templates on the classpath based on a root directory. This can be done
> on the filesystem and, IMHO, should also be possible on the classpath.
>
> Thanks,
> -Nate
>
>
> Harry Karadimas wrote:
>
>> Put your files in /WEB-INF/classes, and try "/" in front of your path,
>> e.g.
>> */*skins/redtheme/etc.st
>> Harry Karadimas
>> /______________________________________________________________________
>> Dr Harry Karadimas Medecin Ingenieur resp. Recherche et Developpement
>> Departement d'Information Hospitalier
>> CHU Henri Mondor 51, av. du Mal de Lattre de Tassigny 94010 CRETEIL
>> tel : (00 33 1) 49 81 21 79 fax : (00 33 1) 49 81 27 08
>> secr.: (00 33 1) 49 81 23 82 m.el.:harry.karadimas at hmn.ap-hop-paris.fr
>> /
>>
>>
>> stringtemplate-interest-request at antlr.org a écrit :
>>
>>> Send stringtemplate-interest mailing list submissions to
>>> stringtemplate-interest at antlr.org
>>>
>>> To subscribe or unsubscribe via the World Wide Web, visit
>>> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
>>> or, via email, send a message with subject or body 'help' to
>>> stringtemplate-interest-request at antlr.org
>>>
>>> You can reach the person managing the list at
>>> stringtemplate-interest-owner at antlr.org
>>>
>>> When replying, please edit your Subject line so it is more specific
>>> than "Re: Contents of stringtemplate-interest digest..."
>>>
>>>
>>> Today's Topics:
>>>
>>> 1. use template groups in a WAR (Nate)
>>>
>>>
>>> ----------------------------------------------------------------------
>>>
>>> Message: 1
>>> Date: Fri, 10 Nov 2006 17:44:23 -0800
>>> From: Nate <misc at n4te.com>
>>> Subject: [stringtemplate-interest] use template groups in a WAR
>>> To: stringtemplate-interest at antlr.org
>>> Message-ID: <45552AF7.4080904 at n4te.com>
>>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>>>
>>> I am writing a web application. I want to use StringTemplate to generate
>>> the HTML pages. I want users to be able to select a "skin" from a list.
>>> Each skin would use a different set of templates to display the HTML.
>>>
>>> My initial impression is that I'd use a different StringTemplateGroup
>>> for each skin. I imagined a file structure like this...
>>>
>>> skins/redtheme/page.st
>>> skins/redtheme/etc.st
>>> skins/bluetheme/page.st
>>> skins/bluetheme/etc.st
>>>
>>> ...the code would be something like...
>>>
>>> skinName = "redtheme";
>>> group = new StringTemplateGroup(skinName, "skins");
>>> group.getInstanceOf("page");
>>> group.getInstanceOf("etc");
>>>
>>> This works fine on the filesystem. Now I zip my application up into a
>>> WAR file and suddenly the templates cannot be found. Next I think I
>>> should look up templates using the classpath. StringTemplate
>>> documentation leads me to think I need to use code like this...
>>>
>>> skinName = "redtheme";
>>> group = new StringTemplateGroup("allSkins");
>>> group.getInstanceOf("skins/" + skinName + "/page");
>>> group.getInstanceOf("skins/" + skinName + "/etc");
>>>
>>> There is no "rootDir" when using the classpath, so I cannot use the a
>>> StringTemplateGroup for each skin, like I could on the filesystem.
>>>
>>> What is the solution? How do other people do it? Why does it work so
>>> differently when JARed or WARed?
>>>
>>> Regards,
>>> Nathan Sweet