develog

[thymeleaf] Layout Dialect 본문

카테고리 없음

[thymeleaf] Layout Dialect

냐옴 2022. 10. 22. 12:21

 

의존성을 추가한다
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>

 

레이아웃 파일을 만든다 (layout1.html)
<!-- src/main/resources/templates/layout/layout1.html -->

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta charset="UTF-8">
    <title>layout1</title>
</head>
<body>

    <!-- layout1 -->

    <!-- page1 content 를 출력한다 -->
    <th:block layout:fragment="content"></th:block>
    <!-- //page1 content 를 출력한다 -->

</body>
</html>

 

개별 페이지 파일 (page1.html)

layout 네임스페이스를 추가하고

<html
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
>

layout1.html 파일을 사용하도록 지정한다

<html
    layout:decorate="~{layout/layout1}"
>

전체 소스

<!-- src/main/resources/templates/page1.html -->

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/layout1}">
<head>
    <meta charset="UTF-8">
    <title>page1</title>
</head>
<body>

    <h2>page1</h2>

    <!-- page1 에서 사용할 content 를 정의한다 -->
    <div layout:fragment="content">
        <p>private content</p>
    </div>


</body>
</html>

 

컨트롤러에서 개별 페이지를 호출한다
@GetMapping("/page1")
public String page1() {
    return "page1";
}

 

 

https://github.com/ultraq/thymeleaf-layout-dialect

 

GitHub - ultraq/thymeleaf-layout-dialect: A dialect for Thymeleaf that lets you build layouts and reusable templates in order to

A dialect for Thymeleaf that lets you build layouts and reusable templates in order to improve code reuse - GitHub - ultraq/thymeleaf-layout-dialect: A dialect for Thymeleaf that lets you build lay...

github.com

 

 

Comments