thymeleaf_layout

ํ…œํ”Œ๋ฆฟ ๋ ˆ์ด์•„์›ƒ 1

ํ…œํ”Œ๋ฆฟ์—์„œ ์ผ๋ถ€ ์ฝ”๋“œ ์กฐ๊ฐ์„ ๊ฐ€์ง€๊ณ  ์™€์„œ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด ์ฝ”๋“œ ์กฐ๊ฐ์„ ๋ ˆ์ด์•„์›ƒ์— ๋„˜๊ฒจ์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด <head>์— ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” css, javascript๊ฐ™์€ ๊ณตํ†ต ์ •๋ณด๋“ค์„ ํ•œ ๊ณณ์— ๋ชจ์•„๋‘๊ณ  ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ฐ ํŽ˜์ด์ง€๋งˆ๋‹ค ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๋” ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์ปจํŠธ๋กค๋Ÿฌ

@GetMapping("/layout")
    public String layout() {
        return "template/layout/layoutMain";
    }
  • layout_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>
  • 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>
  • ๋ Œ๋”๋ง ๋œ HTML

<!DOCTYPE html>
<html>
<head>
    <title>๋ฉ”์ธ ํƒ€์ดํ‹€</title>
    <!-- ๊ณตํ†ต -->
    <link rel="stylesheet" type="text/css" media="all" href="/css/ awesomeapp.css">
    <link rel="shortcut icon" href="/images/favicon.ico">
    <script type="text/javascript" src="/sh/scripts/codebase.js"></script>
    
    <!-- ์ถ”๊ฐ€ -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/themes/smoothness/jquery-ui.css">
</head>
<body>
๋ฉ”์ธ ์ปจํ…์ธ 
</body>
</html>
  • common_header(~{::title},~{::link})

    • ํ˜„์žฌ ํŽ˜์ด์ง€์˜ title ํƒœ๊ทธ์™€ link ํƒœ๊ทธ๋“ค์„ ์ „๋‹ฌํ•œ๋‹ค.

ํ…œํ”Œ๋ฆฟ ๋ ˆ์ด์•„์›ƒ 2

<head>๋ฟ๋งŒ ์•„๋‹ˆ๋ผ HTML ์ „์ฒด์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์ปจํŠธ๋กค๋Ÿฌ

 @GetMapping("/layoutExtend")
    public String layoutExtend() {
        return "template/layoutExtend/layoutExtendMain";
    }
  • layoutFile.html

<!DOCTYPE html>
<html th:fragment="layout (title, content)" xmlns:th="http://www.thymeleaf.org">
<head>
    <title th:replace="${title}">๋ ˆ์ด์•„์›ƒ ํƒ€์ดํ‹€</title>
</head>
<body>
<h1>๋ ˆ์ด์•„์›ƒ H1</h1>
<div th:replace="${content}">
    <p>๋ ˆ์ด์•„์›ƒ ์ปจํ…์ธ </p>
</div>
<footer>
    ๋ ˆ์ด์•„์›ƒ ํ‘ธํ„ฐ
</footer>
</body>
</html>
  • layoutExtendMain.html

<!DOCTYPE html>
<html th:replace="~{template/layoutExtend/layoutFile :: layout(~{::title}, ~{::section})}"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>๋ฉ”์ธ ํŽ˜์ด์ง€ ํƒ€์ดํ‹€</title>
</head>
<body>
<section>
    <p>๋ฉ”์ธ ํŽ˜์ด์ง€ ์ปจํ…์ธ </p>
    <div>๋ฉ”์ธ ํŽ˜์ด์ง€ ํฌํ•จ ๋‚ด์šฉ</div>
</section>
</body>
</html>
  • ๋ Œ๋”๋ง ๋œ HTML

<!DOCTYPE html>
<html>
<head>
    <title>๋ฉ”์ธ ํŽ˜์ด์ง€ ํƒ€์ดํ‹€</title>
</head>
<body>
<h1>๋ ˆ์ด์•„์›ƒ H1</h1>
<section>
    <p>๋ฉ”์ธ ํŽ˜์ด์ง€ ์ปจํ…์ธ </p>
    <div>๋ฉ”์ธ ํŽ˜์ด์ง€ ํฌํ•จ ๋‚ด์šฉ</div>
</section>
<footer>
    ๋ ˆ์ด์•„์›ƒ ํ‘ธํ„ฐ
</footer>
</body>
</html>

Last updated