Spring Boot 4. Thyme의 레이아웃

 Spring Boot 4. Thyme의 레이아웃



1.  include 방식의 처리

th:insert 나 th:replace 를 이용해서 특정한 부분을 다른 내용으로 변경

replace 는 내용을 완전히 대체하고 insert 는 기존 내용의 바깥쪽 태그는 유지하면서 내용만 추가되는 방식



- 불러올 내용 작성

불러올 태그의 th:fragment 속성에 프래그먼트 이름 지정(불러올 때 사용)


- 내용 불러오기

불러올 위치의 태그의 th:replace나 th:insert 속성에 ~{ } 안에 불러올 내용이 있는 패이지의 위치의 경로와 파일명 작성 (resources 폴더 안의 tamplates 폴더가 시작 위치)

경로 다음에 한 칸 띄고 :: 다음에 불러올 프래그먼트의 이름 작성

이름을 지정하지 않으면 해당 패이지 전체를 불러옴


* 예시

불러올 페이지 (templates 폴더 안의 fragments 폴더 안의 fragment1.html 파일)

<div th:fragment="part1">
<h2>Part 1</h2>
</div>

<div th:fragment="part2">
<h2>Part 2</h2>
</div>

<div th:fragment="part3">
<h2>Part 3</h2>
</div>


불러오는 페이지

<h1>Layout 1</h1>
<div th:replace="~{/fragments/fragment1 :: part1}"></div>

<h1>Layout 2</h1>
<div th:insert="~{/fragments/fragment1 :: part2}"></div>

<h1>Layout 3</h1>
<th:block th:replace="~{/fragments/fragment1 :: part3}"></th:block>

<실행결과>











 replace를 이용한 Layout1과 Layout3은 기존의 <div>태그와 <th:block>태그 대신에 불러온 조각들이 사용됨. 

insert를 이용한 Layout2는 기존의 <div> 태그 안에 불러온 <div> 태그가 추가됨



* 예시 - 전체 내용 불러오기

불러올 페이지

<div>

<h2>Fragment 2</h2>
<h2>Fragment 2</h2>
<h2>Fragment 2</h2>

</div>

불러오는 페이지

<h1>Layout Test</h1>
<h2>전체 페이지 불러오기</h2>

<div style="border: 1px solid black">

<th:block th:replace="~{/fragments/fragment2}"></th:block>

</div>

<실행결과>









-> ':: [프래그먼트 이름]' 을 작성하지 않았기 때문에 코드를 보면 전체 페이지의 내용이 추가된 것을 볼 수 있음




2. 파라미터 방식의 처리

불러오는 페이지에서 불러올 페이지를 부르면서 파라미터를 넘겨줄 수 있음

특정한 태그를 파라미터처럼 전달해서 다른 th:fragment 에서 사용가능 (css 의 id 선택자(#) 등 이용)

파라미터로 받은 태그는 th:replace나 th:insert를 이용해서 적용


- 불러올 페이지 : th:fragment 속성의 이름을 지정하고 그 뒤에 ( ) 안에 파라미터를 받아올 변수의 이름 작성

- 불러오는 페이지 : th:replace나 th:insert를 이용해서 다른 내용을 불러올 때 불러올 프래그먼트 이름 다음에 ( ) 안에 전달할 파라미터 값 작성 



* 예시 

불러올 페이지 (fragment3.html)

<div th:fragment="target(first, second)">

<style>
.c1 {
background-color: blue;
}
.c2{
background-color: red;
}
</style>

<div class="c1">
<th:block th:replace = "${first}"></th:block>
</div>

<div class="c2">
<th:block th:replace = "${second}"></th:block>
</div>

</div>

프래그먼트 이름 다음에 파라미터를 받을 변수 작성

태그가 파라미터로 전달되면 페이지 내에서 th:replace를 이용해서 적용


불러오는 페이지

<th:block th:replace="~{/fragments/fragment3 :: 
                                 target(~{this:: #ulFirst}, ~{this :: #ulSecond})}">

<ul id="ulFirst">
<li>AA</li>
<li>BB</li>
<li>CC</li>
</ul>

<ul id="ulSecond">
<li>DD</li>
<li>EE</li>
<li>FF</li>
</ul>

</th:block>

불러올 페이지에 파라미터로 자신의 태그의 아이디(#~~)를 전달


<실행결과>








-> 자신의 태그를 파라미터로 전달해서 적용된 프래그먼트를 불러옴




3. 레이아웃 템플릿


레이아웃 전체를 하나의 페이지로 구성

필요한 부분을 파라미터로 전달

페이지의 header나 footer과 같이 공통적인 부분을 레이아웃으로 만드로 body에 해당하는 부분은 파라미터로 받아와서 화면에 표시



* 예시

layout1.html

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

<th:block th:fragment="setContent(content)">
<head>
<meta charset="UTF-8">
<title>Layout</title>
</head>

<body>
<style>
* {
margin: 0;
padding: 0;
}
.header {
width: 100vw;
height: 20vh;
background-color: aquamarine;
}
.content {
width: 100vw;
height: 70vh;
background-color: beige;
}
.footer {
width: 100vw;
height: 10vh;
background-color: cadetblue;
}
</style>

<div class="header">
<h1>HEADER</h1>
</div>

<div class="content">
<th:block th:replace="${content}"></th:block>
</div>

<div class="footer">
<h1>FOOTER</h1>
</div>
</body>

</th:block>
</html>

-> 간단한 레이아웃 구현. 

<th:block> 태그로 <head> <body> 태그를 감쌈.  

fragment로 지정해서 다름 파일에서 불러올 수 있도록 하고 fragment 이름을 setContent로 지정하고 content로 파라미터를 맏음

class가 content인 <div> 태그는 th:replace 속성을 지정해서 content라는 이름으로 전달되는 파라미터로 대체됨


testLayout.html

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

<th:block th:replace="~{/layout/layout1 :: setContent(~{this :: content})}">

<th:block th:fragment="content">
<h1>layout 테스트</h1>
</th:block>

</th:block>
</html>

가장 바깥의 <th:block 태그는  setContent로 대체됨. 이때 content라는 이름으로 내부의 class가 content인 태그를 전달


<실행결과>









-> testLayout.html의 내용이 layout1.html의 내용으로 대체되었고 layout1.html에서 content 부분만 testLayout.html에서 파라미터로 넘겨준 내용으로 표시됨





4. 외부에서 레이아웃을 다운받아서 적용


스타트부스트랩등의 사이트에서 무료나 유료로 레이아웃을 다운받아서 적용할 수 있음

다운받은 파일을 프로젝트 내의 reosurses -> static 폴더로 압축을 풀어서 옮김


* static폴더

정적 파일들(HTML, CSS, Javascript, 이미지 등)을 저장하는 폴더

클래스패스의 루트 경로에 위치

static 폴더에 저장된 파일들은 웹 어플리케이션의 컨텍스트 경로에 상관없이 접근 가능

8080/ 뒤에 바로 해당 파일의 이름을 작성해서 접근 가능







index.html 파일의 내용을 그대로 레이아웃으로 사용할 layout.html 파일에 붙여넣기

Thymeleaf 관련 설정을 해줘야 함


<th:block th:fragment="setContent(content)">

<head>

<head> 태그 위에 <th:block> 태그를 추가해서 파일의 <head> 와 <body>를 모두 감싸고 fragment 속성을 지정해서 해당 패이지의 내용을 다른 패이지에서 불러올 수 있도록 하고 그 과정에서 content라는 이름으로 파라미터를 받아오도록 설정


<head> 안의 CSS 파일의 링크를 불러오는 부분의 href 속성의 앞에 th:를 붙여주고 내용을 @{/   } 으로 감싸줌

파일 아래에 있는 <script> 태그도 <head> 태그 안으로 옮겨주고 마찬가지로 src 속성의 앞에 th:를 붙여주고 내용를 @{/   }감싸줌 (외부 url의 경우에는 해주지 않아도 됨)

<link th:href="@{/css/styles.css}" rel="stylesheet" />

<script th:src="@{/js/scripts.js}"></script>

레이아웃 파일에서 컨텐트가 표시되는 부분의 내용을 지주고 파라미터로 전달받는 content가 표시되도록 설정
<!-- Page content-->
<div class="container-fluid">
<th:block th:replace = "${content}"></th:block>
</div>



해당 레이아웃을 적용할 페이지에서 th:replace 속성을 이용해서 layout.html 파일에서 <th:block th:fragment>로 감쌌던 내용을 불러오고 그러면서 파라미터로 content를 넘겨줌

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

<th:block th:replace="~{/layout/layout :: setContent(~{this :: content})}">

<th:block th:fragment="content">
<h1>layout 테스트</h1>
</th:block>

</th:block>
</html>


<실행결과>










-> 다운 받은 레이아웃 파일에서 content 부분에 원하는 내용 출력








댓글

이 블로그의 인기 게시물

Spring Boot 6. 계시판 프로젝트

Spring Boot 2. Maria 데이터베이스 & Spring Data JPA

Django 10. DB 연동