본문 바로가기
백엔드/타임리프

템플릿

by 김어찐 2021. 9. 13.
728x90

템플릿 조각

insert, replace 속성으로 불러오기 가능

main.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>

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>

 

템플릿 레이아웃

layoutMain.html 의 head 태그 안의 값을 base.html의 head 태그의 common_header 템플릿 함수를 실행시키고 나온 결과값을 layoutMain.html head 태그로 적용한다.

layoutMain.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head th:replace="template/layout/base :: common_header(~{::title},~{::link})">
        <title>메인 타이틀</title>
        <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
        <link rel="stylesheet" th:href="@{/themes/smoothness/jquery-ui.css}">
    </head>
    <body>
        메인 컨텐츠
    </body>
</html>

 

base.html

<html xmlns:th="http://www.thymeleaf.org">
    <head th:fragment="common_header(title,links)">
        <title th:replace="${title}">레이아웃 타이틀</title>
        <!-- 공통 -->
        <link rel="stylesheet" type="text/css" media="all" th:href="@{/css/awesomeapp.css}">
        <link rel="shortcut icon" th:href="@{/images/favicon.ico}">
        <script type="text/javascript" th:src="@{/sh/scripts/codebase.js}"></script>
        <!-- 추가 -->
        <th:block th:replace="${links}" />
    </head>

 

브라우저 결과

728x90

'백엔드 > 타임리프' 카테고리의 다른 글

타임리프 체크박스  (0) 2021.09.15
타임리프 입력 폼 처리  (0) 2021.09.14
타임리프 자바스크립트, JS each  (0) 2021.09.12
타임리프 block  (0) 2021.09.12
타임리프 주석  (0) 2021.09.12