Implementing server-side HTML templating with JSP + JSTL allows for the generation of tables containing user data:
<body>
...
<table>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.department}</td>
</tr>
</c:forEach>
</table>
...
</body>
This is known as template number 1.
When a user submits new data through a form, AJAX sends the data to the server. Upon success, the new data should be dynamically added to the table without any page reload.
To achieve this, template number 2 can be used. It can either be encapsulated within JavaScript or included in the HTML page with "display: none;" and "class='template'".
<tr class="template" style="display: none;">
<td>%user.name%</td>
<td>%user.age%</td>
...
</tr>
...
<script>
$(".template").clone().fillTheTemplateAndAppendToTheTable(user); //just to explain
</script>
The question: Is it possible to avoid duplicating HTML code and have only one universal template in this scenario? If so, how can that be achieved?