Question about Thymeleaf best practices in order to avoid repeating near identical forms

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about Thymeleaf best practices in order to avoid repeating near identical forms

balteo
Hello,

Say I have the following form in a "childminder/new.html" Thymeleaf template:

<div class="panel panel-default">
 <div class="panel-heading">new childminder advertisement</div>
 <div class="panel-body">
	<form th:object="${advertisementInfo}" th:action="@{/advertisement/childminder/new}" method="post" class="form-horizontal">
...

I also have an almost identical form in another "childminder/edit.html" template.

<div class="panel panel-default">
 <div class="panel-heading">edit childminder advertisement</div>
 <div class="panel-body">
	<form th:object="${advertisementInfo}" th:action="@{/advertisement/childminder/edit}" method="post" class="form-horizontal">
...

The portions that vary are very few:
-The value of the th:action attribute
-The title in the div.
-Maybe a few other parts.

By the way, I use the Apache Tiles / Thymeleaf integration module.

What I want to be able to do is reuse the common html code in the form (not shown here) as best I can. I am not sure how to best do that:
-Should I have a String attribute in Tiles with the value of the th:action and the title?
-What is the recommended practice for the above example?

Regards,

Julien.
Reply | Threaded
Open this post in threaded view
|

Re: Question about Thymeleaf best practices in order to avoid repeating near identical forms

Emanuel
Administrator
Are these templates included by the th:include processor?  If so, then I know you can emulate passing parameters by doing this:

<div th:include="childminder/new.html" th:with="title='new', action='new'">

Then you parameterize your template:

<div class="panel panel-default">
 <div class="panel-heading" th:text="${title} + 'childminder advertisement'">childminder advertisement</div>
 <div class="panel-body">
	<form th:object="${advertisementInfo}" th:action="@{'/advertisement/childminder/' + ${action}}" method="post" class="form-horizontal">
...