티스토리 뷰
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
}
}
728x90
반응형
'Java' 카테고리의 다른 글
[Java] Mapstruct 타입이 다른 필드 매핑하기 (0) | 2024.05.01 |
---|---|
[Java] Mapstruct 사용 시 @Builder 패턴 객체 주의 사항 (0) | 2024.04.15 |
[Java] Object Mapping Frameworks 성능 비교 (0) | 2024.04.02 |
[Java] Mockito Framework Spy, ArgumentCaptor (0) | 2024.03.31 |
[Java] Garbage Collector 개념 (0) | 2024.02.19 |
반응형
300x250