Question

Is it possible to use to do a dynamic Thymeleaf include like this:

<div th:each="module : ${report.modules}" th:include="modules/${module.key} :: ${module.key}"></div>

I get a 500 when loading the page: Exception evaluating SpringEL expression: "module.key"

Was it helpful?

Solution

It is possible but you need to rebuild your template little bit. Because th:include is processed before th:each you need wrap div with th:include to iteration tag. Also path to template must be String so you can't do modules/$module.key as it's not produce desired result I suppose. See example below.

<th:block th:each="module : ${report.modules}">
<div th:include="${#strings.concat('modules/', module.key)} :: ${module.key}"></div>
</th:block>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top