Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include a fragment from the same thymeleaf template using "this :: content" or ":: content" not working as expected #466

Closed
t-cst opened this issue Feb 24, 2016 · 1 comment

Comments

@t-cst
Copy link

t-cst commented Feb 24, 2016

Section "8.1 Including template fragments - Defining and referencing fragments" of the docs states that:

"::domselector" or "this::domselector" Includes a fragment from the same template.

But it seems that only works fine making full reference th:include="test-fragment :: contentB".

I've made a small test project to reproduce the issue, it can be found here: https://github.com/t-cst/thymeleaf-springboot-test

test.html

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <ul th:replace="test-fragment :: contentA" />
  </body>
</html>

test-fragment.html

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <ul th:fragment="contentA">
      <li th:each="idx : ${#numbers.sequence(1,4)}" th:switch="${idx}">

        <th:block th:case="1" 
            th:include="test-fragment :: contentB" 
            th:with="strVar = 'One'"/>

        <th:block th:case="2" 
            th:include="this :: contentB"
            th:with="strVar = 'Two'"/>

        <th:block th:case="3" 
            th:include=" :: contentB"
            th:with="strVar = 'Three'"/>

        <th:block th:case="*" 
            th:include="/test-fragment :: contentB" 
            th:with="strVar = 'Other'"/>
      </li>
    </ul>

    <span th:fragment="contentB">
      <span th:text="|value: ${strVar}|" />
    </span>
  </body>
</html>

Output is:

<!DOCTYPE html>
<html>
   <head></head>
   <body>
      <ul>
         <li><span>value: One</span></li>
         <li></li>
         <li></li>
         <li><span>value: Other</span></li>
      </ul>
   </body>
</html>

Case 2 and 3 are missing.

Thymeleaf - User Forum › General Usage › Include a fragment from the same thymeleaf template using "this :: content" or ":: content" not working as expected

@danielfernandez danielfernandez added this to the Thymeleaf 2.1 milestone Feb 25, 2016
@danielfernandez danielfernandez self-assigned this Feb 25, 2016
@danielfernandez
Copy link
Member

This is not exactly a bug, but more precisely a limitation of all versions of Thymeleaf until 2.1. This should not happen in 3.0 (currently in BETA02).

The problem is in the way Thymeleaf <= 2.1 performs fragment insertion: it basically gets the tree of nodes corresponding to the fragment being inserted and hangs that tree onto the DOM of the higher-level template being processed. By the time these nodes are processed, they have no clue about the original template they came from, so this only resolves to the first-level template (the one for which the template engine was originally called).

Again, though, this has changed in 3.0. The new architecture allows markup events to know about the template they come from, and this references inside a fragment should work as you are expecting them to work.

@danielfernandez danielfernandez removed this from the Thymeleaf 2.1 milestone May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants