Thymeleaf 3.0 fragment resolution without html id within a given scope

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Thymeleaf 3.0 fragment resolution without html id within a given scope

There is (there should be...) a fragment call in my code, which take 2 parameters

<div th:insert="path/fileName :: fancyFragment ~{this :: label} , ~{this :: input})">
     placeholder: body of the fragment
    <input ... >
There is an assertion: in the body of the fragment, there will be always exactly one label & one input tag.
But I cannot just type those fragment expressions as method argument, because it will collect (nearly) every label and input in the given template.
The main question that is, how on earth tell thymeleaf, that search is restricted to the body of the fragment? In terminology that would be a search scope.

Bonus question
If we take again the assertion, is there a way to extremely simplify the fragment call, such as there is only one implicit parameter which takes (let's just assume we wrapped the label and input into a div) the given wrapper div as value, and in the fragment definition somehow thymeleaf could extract the label and input DOM element such as: ${arg0_.input}

<div th:fragment="fancyFragment ()">
    <div th:replace="${arg0_.label}">
    <div th:replace="${arg0_.input}">
This prepared example not fully complete, but represents my expectations.

I can see that the bonus question can be quite hard, because nothing has been mapped as arg0, therefore probably an explicit "search" for the wrapper div should be needed in the fancyFragment fragment definition.

If one explicit method argument needed (as wrapper div selector) that would be a treatable fallback solution.

One extra note: giving id values to the DOM elements would not be a proper solution. Let me just emphasize one more time the root of the problem: I am searching for scope, which I can provide to the fragment resolution, DOM resolution, expression resolution whatever we name it, treat the body of the fragment as the searching scope and use it (and only it) to find the DOM elements by the given expressions, selectors.