How can load templates from a group file or directory?

StringTemplate groups either load all templates from a directory or load all templates from a group file (.stg file). This is not particularly great, but a legacy of group files being added much later. A future version should merge these. for now:

content from Rabea Gransberger

  • You may group .st files in a directory
  • You may create a group file (.stg) which contains all the templates.

Here's an example:

public class GroupFiles {
	public static void main(String[] args) throws IOException {
		StringTemplateGroup groupDir = new StringTemplateGroup("theGroup",

		final StringTemplate oneTemplate = groupDir.getInstanceOf("one");
		oneTemplate.setAttribute("class", GroupFiles.class);

		final FileReader reader = new FileReader("templates/group.stg");
		StringTemplateGroup groupFile = new StringTemplateGroup(reader);
		final StringTemplate oneTemplate2 = groupFile.getInstanceOf("one");
		oneTemplate2.setAttribute("class", GroupFiles.class);

Using the following directory structure:

  - group.stg

Contents of the files:




group theGroup;

one(class) ::= <<

two(className) ::= <<