Development

고급 Thymeleaf 기능 및 기능

sonpro 2023. 3. 6. 10:10
반응형

Thymeleaf

Advanced Thymeleaf Features and Functionality

Thymeleaf is a powerful server-side Java template engine that allows developers to create dynamic web pages with ease. It offers a wide range of features and functionality that can help you build complex web applications quickly and efficiently. In this blog post, we will explore some of the advanced Thymeleaf features and functionality that can take your web development skills to the next level.

Fragments

Fragments are reusable pieces of code that can be included in multiple pages. Thymeleaf provides a simple and intuitive way to define fragments using the th:fragment attribute. Here's an example:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>My Page</title>
</head>
<body>
    <div th:fragment="header">
        <h1>Welcome to my website</h1>
    </div>
    <div th:fragment="footer">
        <p>Copyright © 2021 My Website</p>
    </div>
    <div th:replace="fragments/header :: header"></div>
    <div th:replace="fragments/footer :: footer"></div>
</body>
</html>

In this example, we define two fragments: header and footer. We then include them in our page using the th:replace attribute. The :: syntax is used to specify the fragment name and the fragments/ prefix is used to indicate the location of the fragment file.

Layouts

Layouts are a common pattern in web development that allows you to define a common structure for your pages. Thymeleaf provides a powerful layout mechanism that allows you to define a base layout and then extend it in your individual pages. Here's an example:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title th:text="${title}">My Page</title>
</head>
<body>
    <div th:replace="fragments/header :: header"></div>
    <div th:replace="~{::${layout} /body}"></div>
    <div th:replace="fragments/footer :: footer"></div>
</body>
</html>

In this example, we define a base layout that includes a header and a footer. We then use the ~{::${layout} /body} syntax to indicate that the body of our individual pages should be inserted into the layout. We also use the th:text attribute to dynamically set the page title based on a variable passed in from our controller.

Iteration

Thymeleaf provides a powerful iteration mechanism that allows you to loop over collections and perform operations on each element. Here's an example:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>My Page</title>
</head>
<body>
    <ul>
        <li th:each="item : ${items}" th:text="${item}"></li>
    </ul>
</body>
</html>

In this example, we use the th:each attribute to loop over a collection of items and display each one in a list item. The item variable is automatically created by Thymeleaf and represents the current element in the iteration.

Conditional Rendering

Thymeleaf provides a simple and intuitive way to conditionally render elements based on certain conditions. Here's an example:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>My Page</title>
</head>
<body>
    <div th:if="${loggedIn}">
        <p>Welcome back, ${username}!</p>
    </div>
    <div th:unless="${loggedIn}">
        <p>Please log in to continue.</p>
    </div>
</body>
</html>

In this example, we use the th:if and th:unless attributes to conditionally render elements based on whether the user is logged in or not. The ${loggedIn} and ${username} variables are passed in from our controller and represent the user's login status and username, respectively.

Conclusion

Thymeleaf is a powerful and flexible template engine that can help you build complex web applications quickly and efficiently. In this blog post, we explored some of the advanced Thymeleaf features and functionality that can take your web development skills to the next level. From fragments and layouts to iteration and conditional rendering, Thymeleaf has everything you need to create dynamic and engaging web pages. So why not give it a try and see what you can create?

반응형