웹 페이지를 개발할 때는 공통 영역이 많이 있다. 예를 들어서 상단 영역이나 하단 영역, 좌측 카테고리 등등 여러 페이지에서 함께 사용하는 영역들이 있다. 이런 부분을 코드를 복사해서 사용한다면 변경시 여러 페이지를 다 수정해야 하므로 상당히 비효율 적이다. 타임리프는 이런 문제를 해결하기 위해 템플릿 조각과 레이아웃 기능을 지원한다.

 

package hello.thymeleaf.basic;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/template")
public class TemplateController {
 @GetMapping("/fragment")
 public String template() {
 return "template/fragment/fragmentMain";
 }
}

 

 

/resources/templates/template/fragment/footer.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body><footer th:fragment="copy">
 푸터 자리 입니다.
</footer>
<footer th:fragment="copyParam (param1, param2)">
 <p>파라미터 자리 입니다.</p>
 <p th:text="${param1}"></p>
 <p th:text="${param2}"></p>
</footer>
</body>
</html>

th:fragment 가 있는 태그는 다른곳에 포함되는 코드 조각으로 이해하면 된다.


/resources/templates/template/fragment/fragmentMain.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
<h1>부분 포함</h1>
<h2>부분 포함 insert</h2>
<div th:insert="~{template/fragment/footer :: copy}"></div>

<h2>부분 포함 replace</h2>
<div th:replace="~{template/fragment/footer :: copy}"></div>

<h2>부분 포함 단순 표현식</h2>
<div th:replace="template/fragment/footer :: copy"></div>

<h1>파라미터 사용</h1>
<div th:replace="~{template/fragment/footer :: copyParam ('데이터1', '데이터2')}"></div>
</body>
</html>

template/fragment/footer :: copy : template/fragment/footer.html 템플릿에 있는
th:fragment="copy" 라는 부분을 템플릿 조각으로 가져와서 사용한다는 의미이다.

 

 

 

footer.html 의 copy 부분

<footer th:fragment="copy">
 푸터 자리 입니다.
</footer>

 

 

 

 

부분 포함 insert


<div th:insert="~{template/fragment/footer :: copy}"></div>

<h2>부분 포함 insert</h2>
<div>
<footer>
푸터 자리 입니다.
</footer>
</div>

th:insert 를 사용하면 현재 태그( div ) 내부에 추가한다.

 

 

 

 

 

부분 포함 replace


<div th:replace="~{template/fragment/footer :: copy}"></div>

<h2>부분 포함 replace</h2>
<footer>
푸터 자리 입니다.
</footer>

th:replace 를 사용하면 현재 태그( div )를 대체한다.

 

 

 

 

부분 포함 단순 표현식


<div th:replace="template/fragment/footer :: copy"></div>

<h2>부분 포함 단순 표현식</h2>
<footer>
푸터 자리 입니다.
</footer>

~{...} 를 사용하는 것이 원칙이지만 템플릿 조각을 사용하는 코드가 단순하면 이 부분을 생략할 수 있다.

 

 

 

 

 

 

파라미터 사용


다음과 같이 파라미터를 전달해서 동적으로 조각을 렌더링 할 수도 있다.
<div th:replace="~{template/fragment/footer :: copyParam ('데이터1', '데이터2')}"></div>

<h1>파라미터 사용</h1>
<footer>
<p>파라미터 자리 입니다.</p>
<p>데이터1</p>
<p>데이터2</p>
</footer>

 

 

 

footer.html 의 copyParam 부분

<footer th:fragment="copyParam (param1, param2)">
 <p>파라미터 자리 입니다.</p>
 <p th:text="${param1}"></p>
 <p th:text="${param2}"></p>
</footer>

 

 

 

 

출처 : 김영한 MVC2 강의

'Spring > Thymeleaf' 카테고리의 다른 글

Thymeleaf - 템플릿 레이아웃2  (0) 2022.09.05
Thymeleaf - 템플릿 레이아웃1  (0) 2022.09.04
Thymeleaf - 자바스크립트 인라인  (0) 2022.09.04
Thymeleaf - 블록  (0) 2022.09.04
Thymeleaf - 주석  (0) 2022.09.04
복사했습니다!