1

I'm new to StringTemplates and try to figure out how to build a group of templates from a source other than files (it's actually in a database).

I miss something like (pseudocode):

STGroup group = new STGroup();
group.addTemplate("name", args, "... <actual template goes here> ...");

I wasn't able to find anything similar. STGroup.defineTemplate-methods all look like for internal use only or are java-doc'ed "for testing". STGroup.compile takes a Token.

I actually want to write the contents of a group file in Java. STGroupString doesn't look promising performance-wise. We have hundreds of templates, some of them very large. It doesn't make sense to me to render it into group file syntax and then let StringTemplates parse that back to a name, args, contents structure instead of directly passing in that structure.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193

1 Answers1

0

You can use STGroup.defineTemplate(), even if its Javadoc says for testing:

STGroup stGroup = new STGroup();

stGroup.defineTemplate("myTemplate", "is this true? <if (true)>yes<endif>");

// will print "is this true? yes"
System.out.println(stGroup.getInstanceOf("myTemplate").render());
Bastien Jansen
  • 8,756
  • 2
  • 35
  • 53
  • Thanks for the answer. I guess that would work. I switched to FreeMarker meanwhile, because StringTemplates are very poor from its API point of view (it actually doesn't have any abstraction, it's just a couple of classes that do plenty of things). – Stefan Steinegger Jul 06 '20 at 05:48