Created
April 14, 2015 18:46
-
-
Save derkork/119294c2a320a1b4ec7a to your computer and use it in GitHub Desktop.
Thymeleaf Fragments vs JSP custom tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Thymeleaf fragment declaration --> | |
<!-- in fragments/form-row.html --> | |
<div class="row" layout:fragment="form-row(label,id)" | |
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" | |
xmlns:th="http://www.thymeleaf.org"> | |
<!-- You cannot import the dialect namespaces somehow, you have to define all of these in every file --> | |
<div class="large-12 columns"> | |
<label th:for="${id}"><span th:text="${label}">Label</span> | |
<div layout:fragment="content"></div> | |
</label> | |
</div> | |
</div> | |
<!-- JSP Custom Tag Declaration --> | |
<!-- In fragments/form-row.tag --> | |
<!-- You can include a file containing all taglib declarations --> | |
<%@include file="../jsp/_include/taglibs.jsp"%> | |
<%@ attribute name="label" %> | |
<%@ attribute name="id"%> | |
<div class="row"> | |
<div class="large-12 columns"> | |
<label for="${id}"><c:out value="${label}"/> <!-- No extra span tag needed --> | |
<jsp:doBody/> | |
</label> | |
</div> | |
</div> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Thymeleaf fragment usage --> | |
<!-- Again need to import all namespaces --> | |
<div xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" | |
xmlns:th="http://www.thymeleaf.org"> | |
<!-- Path to fragment is hard-coded in every usage --> | |
<!-- Need to specify th:remove to make sure the tag is actually replaced --> | |
<div layout:include="fragments/form-row :: form-row('foo', 'bar')" th:remove="tag"> | |
<div layout:fragment="content"> <!-- Need to wrap the actual content here --> | |
<input name="foo" id="bar" type="text"/> | |
</div> | |
</div> | |
</div> | |
<!-- This is hardly an improvement over just duplicating the fragment code --> | |
<!-- JSP Fragment usage --> | |
<%@include file="../jsp/_include/taglibs.jsp"%> | |
<!-- You can include a file containing all taglib declarations --> | |
<my:form-row name="foo" id="bar"> | |
<input name="foo" id="bar" type="text"/> | |
</my:form-row> | |
<!-- A lot less noise, much more readable --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment