Skip to content
Advertisement

Pass Thymeleaf block content as variable to fragment

I’m trying to create a fragment that represents a card with custom content. I would like to do something like:

<div class="card" th:fragment="myfragment" th:utext="${content}">
</div>

And then use as

<th:block th:replace="myfragment">
   <p>Some custom content that would be the value of 'content'</p>
</th:block>

This would make it a lot easier to work with bigger html that would be kinda ugly to write in an attribute. (Basically I’m looking for a similar functionality to Blade’s views and slots)

EDIT: I’m aware of fragment parameterisation but passing long and complex html code in an attribute is pretty ugly and hard to manage.

A more descriptive example would be a card where the card body is not a p but a table for example.

Advertisement

Answer

Soo, maybe not the best solution but I managed to get this working based on this Other SO thread and this Example code

I’ve created a new dialect so I can say this:

<zms:card header="'ASD Title'">
   <div th:text="${first_name}"></div>
   asdasd card works asdasd
</zms:card>

And it will render this:

<div class="card shadow mb-4">
    <div class="card-header py-3">
        <div class="d-inline-block">
            <h6 class="m-0 font-weight-bold">ASD Title</h6>
        </div>
    </div>
    <div class="card-body">
        <div>Name</div>
        asdasd card works 
    </div>
</div>
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement