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",
				"templates/group");

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

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

Using the following directory structure:
templates

  -group
   -- one.st
   -- two.st
  - group.stg

Contents of the files:

one.st:

$two(className=class.name)$

two.st:

$className$

group.stg:

group theGroup;

one(class) ::= <<
<two(className=class.name)>


two(className) ::= <<
<className>