Behavior differs for "direct fragment rendering" and "fragment inclusion"?

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

Behavior differs for "direct fragment rendering" and "fragment inclusion"?

Antibrumm
Hi
I have a question about the behavior of direct fragment rendering in Thymeleaf Spring 2.1.0.

Are these assumptions correct?
 - If a fragment is included from another template the result does not include the tag where the fragment name was defined.
 - If a fragment is rendered directly from the controller the result contains also the tag of where the fragment name was defined.


If this is true it means that the output is slightly different if a fragment is rendered in one or the other way.

Is there a possibility to control this?

------------------------
Current behavior in 2.1.0

home.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
	<div th:include="messages :: ajax"></div> 
</body>
</html>

messages.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
	<span th:fragment="ajax">Hello</span>
</body>
</html>

@Controller -> return "home"; results in:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
	<div>Hello</div> 
</body>
</html>

@Controller -> return "messages :: ajax"; results in:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
	<span>Hello</span>
</body>
</html>
---------------------

As you can see the output changes from a div to a span. Can we control this?
Reply | Threaded
Open this post in threaded view
|

Re: Behavior differs for "direct fragment rendering" and "fragment inclusion"?

danielfernandez
Administrator
This is the intended behaviour.

When you use "template :: ajax", you will always include the tag containing the th:fragment. But if you want to do the same with an attribute, you can use "th:replace" instead of "th:include".

Note however that th:replace and th:include are also different in that th:replace will actually replace the tag in which you write the "th:replace" attribute itself.

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Behavior differs for "direct fragment rendering" and "fragment inclusion"?

Antibrumm
Thanks daniel for the clarification.

I understand the behavior and once you know it its possible to deal with it easily.

It can have a little side effect which did break my javascript code since if you replace some fragment with an ajax call for a new version you end up with an additional tag.

 In my case i reload certain table rows and expected the answer to contain only the "td"s as i had the fragment declaration on the tr tag.