본문 바로가기

카테고리 없음

타임리프 파편 +1강

templates/template/layout/layoutMain.html 호출

package hello.thymeleaf;

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("layout")
    public String layout(){
        return "template/layout/layoutMain";
    }

}

 

 

파편파일(template/layout/base.html)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
//파편선언 파편명=common_header, 매개변수=title,links
<head th:fragment="common_header(title,links)">
    <meta charset="UTF-8">
    <title th:replace="${title}">가져와서 교체할 타이틀</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}" />//links의 인자로 바뀔예정
</head>
<body>

</body>
</html>

 

#호출되는 파일(templates/template/layout/layoutMain.html 호출)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">


//이중 replace같은 느낌임. 

//common_header(~{::title},~{::link}) -- 파편의 경로가 있어야할 ::의 앞부분이 비어있음
//이경우 ~{::title}은 이 파일의 title태그 전부를 의미함
//같은 이치로 ~{::link}는 이 파일의 link태그 전부를 의미함
<head th:replace="~{template/layout/base :: common_header(~{::title},~{::link})}">
    <title>지정 타이틀</title>
    <title>지정 타이틀2</title>
    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
    <link rel="stylesheet" th:href="@{/themes/smoothness/jquery-ui.css}">
</head>
<body>
메인 컨텐츠
</body>
</html>