[stringtemplate-interest] Web app - read templates from multiple directories
Ran Tavory
rantav at gmail.com
Sun Nov 1 01:49:29 PDT 2009
I've implemented a string template result type for struts2, and so far I'm
very happy with st, but there's one limitation I haven't been able to figure
out.
It seems ST requires all template files to be at the same directory. (or did
I miss anything?...)
So, for medium to large web-apps this is an annoyance. I'd like to be able
to create multiple directories, such as "full_pages" or "layouts" or
"snippets" or "reports" and be able to load multiple .st files from their
common parent directory.
Is there a way to get over this limitation?
Here's the code I have. I'll paste it below, hope formatting goes well:
/**
* Execute this result, using the specified template location.
* <p/>
* The template location has already been interpolated for any variable
* substitutions.
* <p/>
* NOTE: The current implementation is still under development and has
several restrictions.
* <ul>
* <li>All template files must end with .st</li>
* <li>All template files must be at the same directory. No subdirectories
allowed for now,
* sorry</li>
* </ul>
*/
public void doExecute(String location, ActionInvocation invocation)
throwsIOException,
TemplateException {
final HttpServletRequest request = ServletActionContext.getRequest();
final HttpServletResponse response = ServletActionContext.getResponse();
if (!location.startsWith("/")) {
// Create a fully qualified resource name.
// final ActionContext ctx = invocation.getInvocationContext();
final String base = ResourceUtil.getResourceBase(request);
location = base + "/" + location;
}
final String encoding = getEncoding(location);
String contentType = getContentType(location);
if (encoding != null) {
contentType = contentType + ";charset=" + encoding;
}
response.setContentType(contentType);
final String fullyQualifiedLocation =
ServletActionContext.getServletContext().getRealPath(
location);
final File templateFile = new File(fullyQualifiedLocation);
if (!templateFile.exists()) {
log.error("Template file not found: " + fullyQualifiedLocation);
return;
}
final StringTemplateGroup group = new StringTemplateGroup("webpages",
templateFile.getParent());
String fileName = templateFile.getName();
if (fileName.endsWith(".st")) {
fileName = fileName.substring(0, fileName.length() - ".st".length());
}
final StringTemplate template = group.getInstanceOf(fileName);
final Map<String, Object> model = createModel(invocation);
template.setAttributes(model);
// Output to client
final Writer responseWriter = response.getWriter();
final StringTemplateWriter templateWriter =
newNoIndentWriter(responseWriter);
template.write(templateWriter);
// Flush'n'close
responseWriter.flush();
responseWriter.close();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20091101/f69dc02f/attachment-0001.html
More information about the stringtemplate-interest
mailing list