티스토리 뷰

Java

[Java] Mustache Template Rendering

snail voyager 2024. 4. 3. 00:06
728x90
반응형

com.samskivert:mustache

Mustache 템플릿 엔진의 Java 구현체입니다. 이 라이브러리를 사용하면 Spring 프로젝트에서 Mustache 템플릿을 쉽게 렌더링할 수 있습니다.

기본적인 템플릿 렌더링 기능 외에도 사용자 정의 헬퍼를 추가하여 템플릿 기능을 확장할 수 있습니다.

https://github.com/samskivert/jmustache

implementation 'com.samskivert:jmustache:1.15'

src/main/resources/templates/index.mustache

<!DOCTYPE html>
<html>
<head>
    <title>{{title}}</title>
</head>
<body>
    <h1>{{message}}</h1>
</body>
</html>

Controller.java

import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

@Controller
public class HomeController {

    @Autowired
    private ResourceLoader resourceLoader;

    @GetMapping("/")
    public String index(Model model) throws IOException {
        // Load the template file
        InputStreamReader reader = new InputStreamReader(resourceLoader.getResource("classpath:templates/index.mustache").getInputStream());

        // Compile the template
        Template template = Mustache.compiler().compile(reader);

        // Create the data model
        Map<String, Object> data = new HashMap<>();
        data.put("title", "Mustache Example");
        data.put("message", "Hello, Mustache!");

        // Render the template with the data model
        String rendered = template.execute(data);

        // Add the rendered HTML to the model
        model.addAttribute("content", rendered);

        return "renderedTemplate"; // a view that displays the rendered content
    }
}

 

com.github.spullara.mustache 

Java용 Mustache 템플릿 엔진으로, 간결하고 직관적인 템플릿 기능을 제공

https://github.com/spullara/mustache.java

implementation 'com.github.spullara.mustache.java:compiler:0.9.6'

src/main/resources/templates/index.mustache

<!DOCTYPE html>
<html>
<head>
    <title>{{title}}</title>
</head>
<body>
    <h1>{{message}}</h1>
</body>
</html>

Controller.java

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
public class HomeController {

    @Autowired
    private ResourceLoader resourceLoader;

    @GetMapping("/")
    public String index(Model model) throws IOException {
        // Load the template file
        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile("templates/index.mustache");

        // Create the data model
        Map<String, Object> data = new HashMap<>();
        data.put("title", "Mustache Example");
        data.put("message", "Hello, Mustache!");

        // Render the template with the data model
        StringWriter writer = new StringWriter();
        mustache.execute(writer, data).flush();
        String rendered = writer.toString();

        // Add the rendered HTML to the model
        model.addAttribute("content", rendered);

        return "renderedTemplate"; // a view that displays the rendered content
    }
}

 

 

https://www.baeldung.com/mustache

728x90
반응형
반응형
300x250