1. 介绍

官网

文档

StringTemplate is a java template engine (with ports for C#,Objective-C, JavaScript, Scala) for generating source code, web pages, emails, or any other formatted text output. StringTemplate is particularly good at code generators, multiple site skins, and internationalization / localization.

StringTemplate是一种基于java的模板引擎库。

2. JAVA使用

Using StringTemplate with Java

添加依赖

1
2
3
4
5
<dependency>
<groupId>org.antlr</groupId>
<artifactId>ST4</artifactId>
<version>4.0.8</version>
</dependency>

example

1
2
3
4
5
6
7
8
9
import org.stringtemplate.v4.*;

public class Hello {
public static void main(String[] args) {
ST hello = new ST("Hello, <name>");
hello.add("name", "World");
System.out.println(hello.render());
}
}

out

1
Hello, World

3. 语法

JAVA API documentation

Using StringTemplate To Generate Web Pages

语法介绍(表格)

StringTemplate cheat sheet

StringTemplate 4学习笔记

模板渲染工具StringTemplate

[stringtemplate-interest] Simple example of list iteration

how do I iterate though a java list in stringtemplate?

4. 项目代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.company;

import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.*;
import java.util.Arrays;

public class Args {
private String name;
private String nametype;
private String namejson;

public Args(String name, String nametype) {
this.name = name;
this.nametype = nametype;
this.namejson = name.toLowerCase();
}

public String getNametype() { return nametype; }
public String getName() { return name; }
public String getNamejson() {return namejson; }

public static void main(String[] args) throws IOException {
Args[] parms = new Args[] {
new Args("BID", "string"),
new Args("Species", "string"),
new Args("Color", "string"),
new Args("Weight", "float64"),
new Args("NetWeight", "float64"),
new Args("Owner", "string"),
new Args("Status", "int"),
};

STGroup stg = new STGroupFile("testplayer.stg");
ST s = stg.getInstanceOf("sqlTemplate");
s.add("parms", Arrays.asList(parms));
System.out.println(s.render());

Args[] parms2 = new Args[] {
new Args("SlicedID", "string"),
new Args("BID", "string"),
new Args("Weight", "float64"),
new Args("Desc", "string"),
new Args("Status", "int"),
};
ST s2 = stg.getInstanceOf("sqlTemplate2");
s2.add("parms2", Arrays.asList(parms2));
System.out.println(s2.render());

FileOutputStream out = new FileOutputStream(new File("template1.go"));
out.write(s.render().getBytes());
out.close();

FileOutputStream out2 = new FileOutputStream(new File("template2.go"));
out2.write(s2.render().getBytes());
out2.close();

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
sqlTemplate(parms)
::= <<
type Beef struct {
<parms:{ parm |<parm.name> <\t> <parm.nametype> <\t> `json:"<parm.namejson>"` <\n>}>
}
>>

sqlTemplate2(parms2)
::= <<
type SlicedBeef struct {
<parms2:{ parm |<parm.name> <\t> <parm.nametype> <\t> `json:"<parm.namejson>"` <\n>}>
}
>>