spring mvc - Thymeleaf: how to include page specific javascript using layouts? -
using thymeleaf there way decorate layout w/ page specific javascript , javascript includes?
<!--my layout --> <!doctype html> <html> <head> </head> <body> <div th:replace="fragments/header :: header"> </div> <div class="container"> <div layout:fragment="content"> </div> </div> </body> </html> <!--my page --> <!doctype html> <html layout:decorator="layout"> <head> </head> <body> <div layout:fragment="content"> hello world </div> <script src="pagespecific1.js"></script> <script src="pagespecific2.js"></script> <script> alert("hello world") </script> </body> </html>
in layout template, put fragment
script.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> <body> .. <th:block layout:fragment="script"></th:block> </body> </html>
and in page template, can add script page.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="template.html"> <body> ... <th:block layout:fragment="script"> <script th:src="@{/page.js}"></script> <script> function foo() { ... } </script> </th:block> </body> </html>
don't forget set layout:decorator
in page template.
Comments
Post a Comment