In this article we will use blocks and fragments feature of Thymeleaf to include CSS and javascript files in Thymeleaf template files.
In a template folder of Spring boot application create a folder named as fragments and then create a .html file named as css_js_header.html.
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<th:block th:fragment="cssOnly">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/style.css">
</th:block>
</body>
</html>
In the above we have created fragment block which will be used in thymeleaf tempalte file. For example we are going to use the above block fragment in the following tempalte page
<div th:remove="tag" th:replace="fragments/css_js_header :: cssOnly"></div>
another way is by using
th:block tag
<th:block th:replace="fragments/css_js_header :: cssOnly"></th:block>
Post a Comment
Post a Comment