2

I'm using the template engine StringTemplate for some templates (obviously).

What I want is to be able to store the templates I have in seperate files, ofcourse I can do that with simple .txt files and reading them into a String, looks a bit like this then

ST template = new ST(readTemplateFromFile("template.txt"))

private String readTemplateFromFile(String templateFile){
//read template from file
}

But what I was wondering is if there's functionality in the StringTemplate engine to do that automatically. SO that i don't have to write code that already exists.

I've read something about Group Files but I don't quite understand that,are those like Template files? Or am I completely missing something?

Liam de Haas
  • 1,258
  • 3
  • 18
  • 39
  • Well, you could do `return new String(Files.readAllBytes(Paths.get(templateFile)), StandardCharSets.UTF_8)`. For example. But I would generally assume that your templates would be on the classpath rather than in a file. – Boris the Spider Jan 17 '15 at 16:26
  • @BoristheSpider the question is not how to read the file but if there's functionality in the STringTemplate engine to store templates in files and read them fron files – Liam de Haas Jan 17 '15 at 16:28
  • Here is an example for `stringtemplate v3`: https://theantlrguy.atlassian.net/wiki/display/ST/Five+minute+Introduction – Benny Code Apr 18 '16 at 14:30

2 Answers2

2

Yes, there is functionality available that can be used directly without providing your own file loading code.

From the ST JavaDoc:

To use templates, you create one (usually via STGroup) and then inject attributes using add(java.lang.String, java.lang.Object). To render its attacks, use render().

To follow that advice the following code can be used.

First, create a file called exampleTemplate.stg and place it on your classpath.

templateExample(param) ::= <<
This is a template with the following param: (<param>)
>>

Then, render the template by using the following code:

// Load the file
final STGroup stGroup = new STGroupFile("exampleTemplate.stg");

// Pick the correct template
final ST templateExample = stGroup.getInstanceOf("templateExample");

// Pass on values to use when rendering
templateExample.add("param", "Hello World");

// Render
final String render = templateExample.render();

// Print
System.out.println(render);

The output is:

This is a template with the following param: (Hello World)


Some additional notes:

  • STGroupFile is a subclass of STGroup. There are other subclasses as well that you find out more about in the JavaDoc.
  • In the example above the template file was placed on the classpath. This is not a requirement, files can be placed in a relative folder or a in an absolute folder as well.
wassgren
  • 18,651
  • 6
  • 63
  • 77
0

An alternative is to use Java's String Templates feature. It is described in JEP 430, and it appears in JDK 21 as a preview feature. Here is an example use:

String name = "Joan";
String info = STR."My name is \{name}";
assert info.equals("My name is Joan");   // true

Java's string templates are more versatile, and much safer, than the interpolation found in other languagues such as Python's f-strings. For example, string concatenation or interpolation makes SQL injection attacks possible:

String query = "SELECT * FROM Person p WHERE p.last_name = '" + name + "'";
ResultSet rs = conn.createStatement().executeQuery(query);

but this variant (from JEP 430) prevents SQL injection:

PreparedStatement ps = DB."SELECT * FROM Person p WHERE p.last_name = \{name}";
ResultSet rs = ps.executeQuery();
mernst
  • 7,437
  • 30
  • 45