Liam de Haas
Liam de Haas

Reputation: 1268

Read templates from a file StringTemplate

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?

Upvotes: 2

Views: 10413

Answers (2)

mernst
mernst

Reputation: 8117

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();

Upvotes: 0

wassgren
wassgren

Reputation: 19201

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.

Upvotes: 2

Related Questions