Skip to content

Instantly share code, notes, and snippets.

@moravcik
Created March 7, 2014 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moravcik/9412935 to your computer and use it in GitHub Desktop.
Save moravcik/9412935 to your computer and use it in GitHub Desktop.
Example code for Thymeleaf nested layout:replace problem. Download all files and run with: 'groovy ThymeleafNestedLayoutProblem.groovy'
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<!--/* accordion-group layout defined */-->
<div layout:fragment="accordion-group" th:remove="all">
<div>accordion header</div>
<div layout:fragment="accordion-inner">accordion inner X</div>
</div>
<!--/* accordion-group layout used */-->
<div layout:include="nested-layout-problem::accordion-group">
<div layout:fragment="accordion-inner">accordion inner
<th:block th:with="i=1">
<!--/* contractList layout defined and used */-->
<div layout:fragment="contractList(i)">
<th:block th:text="|contract list ${i}|"></th:block>
<th:block layout:fragment="removeEmailFromList">remove from 1</th:block>
</div>
<!--/* contractList layout used */-->
</th:block>
<div layout:replace="nested-layout-problem::contractList(2)">
<th:block layout:fragment="removeEmailFromList">remove from 2</th:block>
</div>
<!--/* contractList layout used */-->
<div layout:replace="nested-layout-problem::contractList(3)">
<th:block layout:fragment="removeEmailFromList">remove from 3</th:block>
</div>
</div>
</div>
</html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<!--/* layout defined */-->
<div layout:fragment="accordion-group" th:remove="all">
<div>accordion header</div>
<div layout:fragment="accordion-inner">accordion inner X</div>
</div>
<!--/* nested layout defined */-->
<div layout:fragment="contractList(i)" th:remove="all">
<th:block th:text="|contract list ${i}|"></th:block>
<th:block layout:fragment="removeEmailFromList">remove from X</th:block>
</div>
<!--/* layout used */-->
<div layout:include="nested-layout-problem::accordion-group">
<div layout:fragment="accordion-inner">accordion inner
<!--/* nested layout used */-->
<div layout:include="nested-layout-working::contractList(1)">
<th:block layout:fragment="removeEmailFromList">remove from 1</th:block>
</div>
<!--/* nested layout used */-->
<div layout:include="nested-layout-working::contractList(2)">
<th:block layout:fragment="removeEmailFromList">remove from 2</th:block>
</div>
<!--/* nested layout used */-->
<div layout:include="nested-layout-working::contractList(3)">
<th:block layout:fragment="removeEmailFromList">remove from 3</th:block>
</div>
</div>
</div>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<th:block th:with="i=1">
<!--/* contractList layout defined and used */-->
<div layout:fragment="contractList(i)" th:inline="text">
<th:block th:text="|contract list ${i}|"></th:block>
<th:block layout:fragment="removeEmailFromList">remove from 1</th:block>
</div>
</th:block>
<!--/* contractList layout used */-->
<div layout:replace="not-nested-layout::contractList(2)">
<th:block layout:fragment="removeEmailFromList">remove from 2</th:block>
</div>
<!--/* contractList layout used */-->
<div layout:replace="not-nested-layout::contractList(3)">
<th:block layout:fragment="removeEmailFromList">remove from 3</th:block>
</div>
</html>
import nz.net.ultraq.thymeleaf.LayoutDialect
import org.thymeleaf.TemplateEngine
import org.thymeleaf.context.Context
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
@Grab(group='org.slf4j', module='slf4j-simple', version='1.6.1')
@Grab(group='org.thymeleaf', module='thymeleaf', version='2.1.2.RELEASE')
@Grab(group='nz.net.ultraq.thymeleaf', module='thymeleaf-layout-dialect', version='1.2.2')
def TemplateEngine getTemplateEngine() {
new TemplateEngine(
templateResolver: new ClassLoaderTemplateResolver(suffix: ".html"),
additionalDialects: new LayoutDialect() as Set)
}
def templateEngine = getTemplateEngine()
def resultNotNested = templateEngine.process("not-nested-layout", new Context())
def resultNested = templateEngine.process("nested-layout-problem", new Context())
def resultNestedOk = templateEngine.process("nested-layout-working", new Context())
def removeEmptyLines = { String str -> str.replaceAll(/(?m)^\t*\n/, "") }
println "\nNO PROBLEM HERE:\n${removeEmptyLines resultNotNested}"
println "\nPROBLEM WHEN NESTED:\n${removeEmptyLines resultNested}"
println "\nWORKS WHEN DEFINED SEPARATELY:\n${removeEmptyLines resultNestedOk}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment