Skip to content

Instantly share code, notes, and snippets.

@moravcik
Created March 7, 2014 14:56

Revisions

  1. moravcik created this gist Mar 7, 2014.
    27 changes: 27 additions & 0 deletions ThymeleafNestedLayoutProblem.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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}"
    32 changes: 32 additions & 0 deletions nested-layout-problem.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    <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>
    35 changes: 35 additions & 0 deletions nested-layout-working.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <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>
    20 changes: 20 additions & 0 deletions not-nested-layout.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <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>