타임리프에서 제공하는 <th:block> (블록 태그)는 HTML 태그가 아닌 타임리프의 유일한 자체 태그이다.
바로 코드로 살펴보자.
BasicController
@GetMapping("/block")
public String block(Model model){
addUsers(model);
return "basic/block";
}
private void addUsers(Model model){
List<User> list = new ArrayList<>();
list.add(new User("UserA", 10));
list.add(new User("UserB", 20));
list.add(new User("UserC", 30));
model.addAttribute("users", list);
}
basic/block.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<th:block th:each="user : ${users}">
<div>
사용자 이름1 <span th:text="${user.username}"></span>
사용자 나이1 <span th:text="${user.age}"></span>
</div>
<div>
요약 <span th:text="${user.username} + ' / ' + ${user.age}"></span>
</div>
</th:block>
</body>
</html>
타임리프의 특성상 HTML 태그안에 속성으로 기능을 정의해서 사용하는데, 위 예시 처럼 이렇게 사용하기 애매한 경우에 사용하면 된다.
<th:block> 은 렌더링시 제거된다.
해당 기능은 잘 사용하지 않으며, 위에서 말한 것 처럼 단순 반복문으로 해결하기 힘든 경우에만 사용하면 된다.
코드 실행 결과
'Etc > Thymeleaf' 카테고리의 다른 글
Thymeleaf - 템플릿 조각 (0) | 2023.07.25 |
---|---|
Thymeleaf - 자바스크립트 인라인 (0) | 2023.07.25 |
Thymeleaf - 주석 (0) | 2023.07.25 |
Thymeleaf - 반복 (0) | 2023.07.25 |
Thymeleaf - 속성 값 설정 (1) | 2023.07.25 |