LoginSignup
1
0

More than 3 years have passed since last update.

Java で Mustache テンプレートエンジンの JMustache を使うサンプルコード

Last updated at Posted at 2020-08-06

概要

  • Java で Mustache テンプレートエンジンの JMustache を使う
  • 今回の動作確認環境: Java 14 (AdoptOpenJDK 14.0.2+12) + JMustache 1.15 + Gradle 6.5.1 + macOS Catalina

サンプルコード

ソースコード一覧

├── build.gradle
└── src
    └── main
        ├── java
        │   ├── SampleApp.java
        │   └── SampleData.java
        └── resources
            └── my_template.html

build.gradle

plugins {
  id 'application'
}

repositories {
  mavenCentral()
}

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

application {
  mainClassName = 'SampleApp'
}

src/main/java/SampleApp.java

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/main/java/SampleData.java

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/resources/my_template.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</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 で実行

$ gradle run

> Task :run
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</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>

参考資料

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0