在Java中使用Mustache模板引擎JMustache的示例代码


概述

  • 在Java中使用Mustache模板引擎JMustache
  • 这次的操作检查环境:Java 14(AdoptOpenJDK 14.0.2 12)JMustache 1.15 Gradle 6.5.1 macOS Catalina

样本代码

源代码列表

1
2
3
4
5
6
7
8
├── build.gradle
└── src
    └── main
        ├── java
        │   ├── SampleApp.java
        │   └── SampleData.java
        └── resources
            └── my_template.html

build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
plugins {
  id 'application'
}

repositories {
  mavenCentral()
}

dependencies {
  // https://mvnrepository.com/artifact/com.samskivert/jmustache
  implementation 'com.samskivert:jmustache:1.15'
}

application {
  mainClassName = 'SampleApp'
}

src /主/ Java / SampleApp.java

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
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class SampleApp {

  public static void main(String[] args) throws IOException {
    new SampleApp().invoke();
  }

  public void invoke() throws IOException {

    // Mustache テンプレートファイルを読み込む
    String templateText = getResourceText("my_template.html");
    Template template = Mustache.compiler().compile(templateText);

    // オブジェクトを Mustache テンプレートに流し込む
    SampleData data = new SampleData();
    String out = template.execute(data);

    // 結果を出力する
    System.out.println(out);
  }

  // リソースのディレクトリからファイルを読み込む
  public String getResourceText(String path) throws IOException {
    try (InputStream is = getClass().getResourceAsStream(path)) {
      return new String(is.readAllBytes(), StandardCharsets.UTF_8);
    }
  }
}

src /主/ Java / SampleData.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.List;
import java.util.Map;

public class SampleData {

  public String foo = "foo";

  public String getBar() {
    return "bar";
  }

  public String[] strings = {"S1", "S2", "S3"};

  public List list = List.of("L1", "L2", "L3");

  public Map map = Map.of("key1", "value1", "key2", "value2", "key3", "value3");
}

src / main /资源/ my_template.html

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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
Title
</head>
<body>
foo: {{foo}}<br>
getBar(): {{bar}}<br>
<br>
strings:<br>
{{#strings}}
  value: {{.}}<br>
{{/strings}}
<br>
list:<br>
{{#list}}
  value: {{.}}<br>
{{/list}}
<br>
map:<br>
{{#map}}
  {{key1}}, {{key2}}, {{key3}}
{{/map}}
</body>
</html>

在Gradle

上运行

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
$ gradle run

> Task :run
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
Title
</head>
<body>
foo: foo<br>
getBar(): bar<br>
<br>
strings:<br>
  value: S1<br>
  value: S2<br>
  value: S3<br>
<br>
list:<br>
  value: L1<br>
  value: L2<br>
  value: L3<br>
<br>
map:<br>
  value1, value2, value3
</body>
</html>

参考

  • github --samskivert / jmustache:Mustache模板语言的Java实现。
  • 胡须(5)-少逻辑的模板。