Thymeleaf - 블럭(Block)
<th:block>
은 HTML 태그가 아닌 타임리프의 유일한 자체 태그다.
예제
Controller
@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);
}
@Data
static class User {
private String username;
private int age;
public User(String username, int age) {
this.username = username;
this.age = age;
}
}
View
<!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>
은 렌더링시 제거된다.
인프런 김영한님의 Sping강의 기반으로 공부한 것을 작성한 블로그입니다.
출처 : 인프런 -🔗 스프링 MVC 1편 by 우아한형제 김영한이사님
'Thymeleaf > Thymeleaf - 기본기능' 카테고리의 다른 글
Thymeleaf - 템플릿 조각 (0) | 2023.04.03 |
---|---|
Thymeleaf - 자바스크립트 인라인 (0) | 2023.04.03 |
Thymeleaf - 주석 (0) | 2023.04.03 |
Thymeleaf - 조건부 평가 (0) | 2023.04.03 |
Thymeleaf - 속성 값 설정 (0) | 2023.04.03 |