Search code examples
bootstrap-4thymeleaf

How to set th:attribute from fragment


I was wondering if there was a way to set an attribute (th:data-content to be specific) from a fragment.

I want to set bootstrap popover data-content in the thymeleaf template and i would like to keep the content template in a fragment.

I cant think of a way how to do this, maybe it can't be done. I would be very grateful for any input of how to achieve something like this.

Code Example of what I would like to do:

  <button th:data-content="~{fragments :: popover-content}" ...>Set mt data-content from fragment</button>

  <div th:fragment="popover-content" >
    <p>...</p>
  </div>

So probably I need to set the content via javascript, but it would be very nice if there was a way to do this directly in the thymeleaf template.


Solution

  • It does actually work this way. I must have set something up incorrectly, because I tried it again and it works!

    I really like thymeleaf, but documentation can be a bit lacking.

    edit: well I have to take back the part about lacking documentation, it was all there. I will leave the question up, maybe it will be helpful to someone in the future.

    thymeleaf template expression documentation (see section 8 template-layout)