참고: https://catsbi.oopy.io/32a9458e-f452-4733-b87c-caba75f98e2d
타임리프 특징
사용법
${…}: 변수 표현식*{…}: 선택 변수 표현식#{…}: 메세지 표현식@{…}: 링크 URL 표현식~{…}: 조각 표현식+: 문자 합치기| : 리터럴 대체+, -, *, /, %: 이진 연산-: 찾아보기!@@@@and, or: 이진 불린 연산|, &>, <, >=, <= (gt, lt, ge, le): 비교==, != (eq, ne): 동등 연산(if) ? (then): if - then 문법(if) ? (then) : (else): if - then - else 문법(value) ?: (defaultValue): 기본값 설정_: No-Operation텍스트
th:text 활용
<p th:text="${name}"></p>
th:utext
// name 애트리뷰트에 html 태그가 포함된 경우 태그가 text로 나오지 않고 적용됨
// name = '<b>chobolevel</b>' 인 경우 bold서체 적용됨
<p th:utext="${name}"></p>
[[…]] 활용
<p>hello [[${name}]]</p>
[(…)] 활용
// th:inline은 태그안의 내용을 타임리프가 해석하지 않도록 함
// th:utext처럼 사용하기 위해 적용
<p th:inline="none">[(${name})]></p>
지역 변수 선언
<div th:with="item = ${list[0]}">
<p th:text="${item.name}"></p>
<p th:text="${item.age}"></p>
</div>
기본 객체
${#request}${#session}${#response}${#servletContext}${#locale}javascript에서 타임리프 변수 사용방법
1번째 방법
// th:inline="javascript"를 script 태그에 사용하면 별도 주석없이 타임리프 변수 사용
<script th:inline="javascript">
const name = [[${name}]]
console.log(name)
</script>
// 주석을 사용해서 타임리프 변수 사용
<script>
/*<![CDATA[*/
const name = /*[[${name}]]*/
console.log(name)
/*]]>*/
</script>
th:inlnie=javascript 명시/*<![CDATA[*/ /*]]>*/로 감싸야함(th:inline=”javascript” 사용하지 않은 경우)/*[[${…}]]*/에 감싸서 사용2번째 방법
<input id="clientId" type="hidden"
th:value="${@environment.social.app.kakao.client-id}"/>
<script>
const clientId = document.querySelector('#clientId').value;
</script>
3번째 방법

레이아웃
사용하는 이유
dependency
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
사용
fragment 생성
// header.html
<html lang="ko">
<div th:fragment="headerFragment">
<h1>Header Fragment</h1>
</div>
</html>
fragment layout 작성
// default_layout.html
<!DOCTYPE html>
<html lang="ko" xmlns:layout="<http://www.ultraq.net.nz/thymeleaf/layout>">
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
// th:replace="(templates 이후 경로) :: th:fragment로 작성한 이름"
<th:block th:replace="fragments/header :: headerFragment"></th:block>
// html파일별로 아래 부분만 바뀌게 됨
<section layout:fragment="content"></section>
<th:block th:replace="fragments/footer :: footerFragment"></th:block>
</body>
</html>
layout 활용
// index.html
<!DOCTYPE html>
<html lang="ko"
xmlns:layout="<http://www.ultraq.net.nz/thymeleaf/layout>"
layout:decorate="~{layout/default_layout.html}">
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<section layout:fragment="content">
// 작성한 내용이 default_layout.html의 layout:fragment="content" 영역에 작성됨
</section>
</body>
</html>