타임리프 문법

Jiwon Park·2023년 5월 15일

*연산자

텍스트

[[${data}]]
<span th:text="${data}"></span>
<span th:text="(10 % 2 == 0)? '짝수':'홀수'"></span>
<span th:text="${data}?:'abc'">data값이 없으면 'abc' 출력</span>
<span th:text="${data}?: _"> data값이 없으면 html내용 출력</span>

*${{data}}, th:field 는 자동 컨버터 적용됨

url 링크

  <a th:href="@{/basic/text-basic(param1=${param1})}">query param</a>
  <a th:href="@{/hello/{param1}(param1=${param1}, param2=${param2})}">path variable + query parameter</a>

리터럴

<span th:text="'hello world!'"></span>
<span th:text="'hello ' + ${data}"></span>
<span th:text="|hello ${data}|"></span>

checked

- checked o <input type="checkbox" name="active" checked/>
- checked o <input type="checkbox" name="active" th:checked="true" />
- checked x <input type="checkbox" name="active" th:checked="false" />

반복

<table border="1">
  <tr>
    <th>count</th>
    <th>username</th>
    <th>age</th>
    <th>etc</th>
  </tr>
  <tr th:each="user : ${users}">
    <td th:text="${userStat.count}">username</td>
    <td th:text="${user.username}">username</td>
    <td th:text="${user.age}">0</td>
    <td>
      index = <span th:text="${userStat.index}"></span>
      count = <span th:text="${userStat.count}"></span>
      size = <span th:text="${userStat.size}"></span>
      even? = <span th:text="${userStat.even}"></span>
      odd? = <span th:text="${userStat.odd}"></span>
      first? = <span th:text="${userStat.first}"></span>
      last? = <span th:text="${userStat.last}"></span>
      current = <span th:text="${userStat.current}"></span>
    </td>
  </tr>
</table>

반복 상태 유지 기능(지정한 변수명('user') + Stat))
index : 0부터 시작하는 값
count : 1부터 시작하는 값
size : 전체 사이즈
even , odd : 홀수, 짝수 여부( boolean )
first , last :처음, 마지막 여부( boolean )
current : 현재 객체

조건(만족하지 않으면 태그 자체가 렌더링 되지 않고 사라진다.)

<span th:text="'성인'" th:if="${user.age ge 20}"></span>
<span th:text="'성인'" th:unless="${user.age lt 20}"></span>

<td th:switch="${user.age}">
 <span th:case="10">10살</span>
 <span th:case="20">20살</span>
 <span th:case="*">기타</span>
</td>




<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
  
<h1>if, unless</h1>
<table border="1">
 <tr>
   <th>count</th>
   <th>username</th>
   <th>age</th>
 </tr>
 <tr th:each="user, userStat : ${users}">
   <td th:text="${userStat.count}">1</td>
   <td th:text="${user.username}">username</td>
   <td>
     <span th:text="${user.age}">0</span>
     <span th:text="'미성년자'" th:if="${user.age lt 20}"></span>
     <span th:text="'미성년자'" th:unless="${user.age ge 20}"></span>
   </td>
 </tr>
</table>
  
<h1>switch</h1>
<table border="1">
 <tr>
   <th>count</th>
   <th>username</th>
   <th>age</th>
 </tr>
 <tr th:each="user, userStat : ${users}">
   <td th:text="${userStat.count}">1</td>
   <td th:text="${user.username}">username</td>
   <td th:switch="${user.age}">
     <span th:case="10">10살</span>
     <span th:case="20">20살</span>
     <span th:case="*">기타</span>
   </td>
 </tr>
</table>
</body>
</html>

주석

<h1>1. 표준 HTML 주석</h1>
<!--
<span th:text="${data}">html data</span>
-->
<h1>2. 타임리프 파서 주석</h1>
<!--/*
<span th:text="${data}">html data</span>
*/-->

자바스크립트 인라인

<script th:inline="javascript">

템플릿 조각

<footer th:fragment="copyParam (param1, param2)">
 <p th:text="${param1}"></p>
 <p th:text="${param2}"></p>
</footer>

<div th:replace="~{template/fragment/footer :: copyParam ('데이터1', '데이터2')}"></div>

템플릿 레이아웃

<!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>


<!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>
profile
안녕하세요

0개의 댓글