Thymeleaf 3 with nested (recursive) evaluation and th:replace

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Thymeleaf 3 with nested (recursive) evaluation and th:replace

N4zroth
I'm trying to create a Template that allows me to pass an attribute to the model specifying the name of the fragment that will be used to replace a div-tag in my template.

The template code is:
 <div th:replace="${__${T(mypackage.WebUtils).INCLUDE_FRAGMENT_NAME}__}"/>
(the value of INCLUDE_FRAGMENT_NAME is "fragment").

In my controller I have:
model.addAttribute(WebUtils.INCLUDE_FRAGMENT_NAME, WebUtils.MAIN_OVERVIEW_DIV_INCLUDE);
with MAIN_OVERVIEW_DIV_INCLUDE being "main/main :: fragment".

The error I get is this one:
HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/WEB-INF/templates/main/main :: fragment.html]" - line 43, col 14)
I understand that the URL is wrong (or is it?) but I have no idea on how to fix that.
Even when doing
 <div th:replace="${fragment}"/>
, the error persists :( Any help?

Also, it would be nice if I could set the fragment name in main/main dynamically but I guess that's out of the question as the fragment isn't evaluated before being included, right?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf 3 with nested (recursive) evaluation and th:replace

Zemi
Administrator
Hello,

I think you don't need that extra evaluation there (expression + preprocessing).

First, try to put the value directly in the th:replace expression

   <div th:replace="main/main :: fragment" />

to be sure that the expression is right. That expression means that there is a fragment called "fragment" inside a template called "main" inside a folder called "main".

Maybe there is some confusion with the prefix and suffix configured in the TemplateEngine, please check that.

Kind Regards,
  Zemi

Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf 3 with nested (recursive) evaluation and th:replace

N4zroth
This post was updated on .
Hi,
thanks, yea, I tried that - main/main :: fragment worked fine.
The issue seems to be related to https://github.com/thymeleaf/thymeleaf-spring/issues/77 - I changed my code to either th:replace or th:include
<div th:replace="${__${T(mypackage.WebUtils).INCLUDE_FILE_VAR_NAME}__} :: ${__${T(mypackage.WebUtils).INCLUDE_FRAGMENT_VAR_NAME}__}"/>
This no longer results in an error, but the replacement/inclusion doesn't work - replace results in nothing, insert results in an empty div-tag :-(
Any other ideas?
Thanks again :)
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf 3 with nested (recursive) evaluation and th:replace

N4zroth
The problem here seemed to be that I gave the fragment that was to be included a name referencing a public static final String - which worked when including the fragment directly.
I just switched to just including the file and not a fragment - not really the appropriate solution but rather a workaround :(
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf 3 with nested (recursive) evaluation and th:replace

Zemi
Administrator
Hello,

the problem could be with that double expression mechanism.

Assuming that INCLUDE_FILE_VAR_NAME is "foo" and INCLUDE_FRAGMENT_VAR_NAME is "var", try

  <div th:replace="__${foo}__ :: ${bar}__" />

to check that the problem is there.

Regards,
  Zemi