Sample code that uses the Mustache template engine JMustache in Java

Overview

--Using Mustache template engine JMustache in Java --Operation check environment this time: Java 14 (AdoptOpenJDK 14.0.2 + 12) + JMustache 1.15 + Gradle 6.5.1 + macOS Catalina

Sample code

Source code list

├── 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 {

    //Load Mustache template file
    String templateText = getResourceText("my_template.html");
    Template template = Mustache.compiler().compile(templateText);

    //Pour objects into Mustache templates
    SampleData data = new SampleData();
    String out = template.execute(data);

    //Output the result
    System.out.println(out);
  }

  //Read a file from the resource directory
  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>

Run in 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>

Reference material

Recommended Posts

Sample code that uses the Mustache template engine JMustache in Java
Sample code that uses the Mustache template engine in Spring Boot
Guess the character code in Java
Sample code to call the Yahoo! Local Search API in Java
Sample program that returns the hash value of a file in Java
[Java] Implement a function that uses a class implemented in the Builder pattern
Java sample code 02
Java sample code 03
Java sample code 04
Java sample code 01
A bat file that uses Java in windows
Sample source code for finding the least common multiple of multiple values in Java
Code that deletes all files of the specified prefix in AWS S3 (Java)
Sample code to get the values of major SQL types in Java + MySQL 8.0
Sample code to convert List to List <String> in Java Stream
Code that only displays the built-in camera in Processing
Differences in code when using the length system in Java
The story that .java is also built in Unity 2018
Sample code to get the values of major SQL types in Java + Oracle Database 12c
Identify threads in the Java process that are wasting CPU
Correct the character code in Java and read from the URL
Digital signature sample code (JAVA)
Java parallelization code sample collection
Template Engine Pebble (Java) --Guide
Java in Visual Studio Code
Write Java8-like code in Java8
How to implement a job that uses Java API in JobScheduler
Use an example domain for the package name in the sample code
Access the network interface in Java
Java version notation that changes in Java 10
Multithreaded to fit in [Java] template
Java Spring environment in vs Code
Specify the java location in eclipse.ini
Unzip the zip file in Java
Java 9 new features and sample code
A simple sample callback in Java
Parsing the COTOHA API in Java
Sample vending machine made in Java
Sample code using Minio from Java
Call the super method in Java
Sample code to serialize and deserialize Java Enum enums and JSON in Jackson
This and that of the implementation of date judgment within the period in Java
The story that the Servlet could not be loaded in the Java Web application
About the phenomenon that StackOverflowError occurs in processing using Java regular expressions