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

Add attributes after replace #716

Open
NielsNet opened this issue Oct 11, 2018 · 5 comments
Open

Add attributes after replace #716

NielsNet opened this issue Oct 11, 2018 · 5 comments

Comments

@NielsNet
Copy link

Currently (Thymeleaf 3.0.0) there does not seem to be a way to add attributes to a tag that is replaced.
E.g. when having a fragment called testFrag in a file /test/fragments:

<div class="default-class">
   Hello World!
</div>

and using this:

<div th:replace="/test/fragments::testFrag" th:classappend="append-me"></div>

I'd expect the following:

<div class="default-class append-me">
    Hello World!
</div>

However, the actual output is

<div class="default-class">
    Hello World!
</div>

I was wondering if I just missed out a way to do this with standard thymeleaf or if it might be a planned feature.

@danielfernandez
Copy link
Member

This is the expected behaviour of th:replace. This attribute replaces the tag it lives in with the one selected in the expression, it does not merge it or peform any similar operation. So a th:classappend in the tag that contains that th:replace will be simply lost.

In order to do a merge operation like the one you need, you'd have to do something more specific, like for example:

<div th:fragment="testFrag(classapp)" class="default-class" th:classappend="${classapp}">
   Hello World!
</div>

And then:

<div th:replace="/test/fragments :: testFrag ('append-me')"></div>

@NielsNet
Copy link
Author

This would mean that one would have to define a parameter for any attributes to add on the replaced fragment. Is there a way to do something like a merge or would that require a custom dialect?
Imagine you wanted to use some data tags, the classes and some more from the replaced tag on its replacement. It would become a mess with that many parameters.

@Simsteve7
Copy link

Is there any update on the question of @NielsNet? It would be really nice to replace fragments and keep the attributes.

@ygogolitsyn
Copy link

Have been asked to look at this issue by Code Triage.
Would an introduction of a completely new attribute that would do what th:relplace does, but keep the attributes be acceptable? Something like th:replacewithattr?

@srinath-gs
Copy link

srinath-gs commented Aug 8, 2023

Have been asked to look at this issue by Code Triage. Would an introduction of a completely new attribute that would do what th:relplace does, but keep the attributes be acceptable? Something like th:replacewithattr?

is there any update on this th:replacewithattr ? @danielfernandez

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

5 participants