Search code examples
thymeleaf

Dynamic Thymeleaf fragment location


Imagine a tag:

<div th:replace="pageName/page.html :: fragmentName" />

where pageName should come from model like ${page.name}, but I don't know the syntax how I can insert a variable to the beginning of the fragment location.

Any ideas?


Solution

  • According to this, you can simply use variables in fragment expressions. (The example given is ~{${templateName} :: ${fragmentName}}. This should work for you:

    <div th:replace="~{|pageName/${page.name}.html| :: fragmentName}" />