There is a recurring issue that I often encounter where I find myself duplicating markup, and I am unsure of how to address it. Let me present a common scenario:
Imagine we want to add comments to an article. Below the article content, there are existing comments generated by the templating engine (such as Freemarker or PHP) when the page was initially loaded.
Whenever a user adds a new comment, we need to create a new li element and include it in the list of comments on the current page. This li will contain various elements like:
- The user's avatar
- Their name
- A link to their profile or to send a private message
- The text of their comment
- The date of the comment
- "Edit" and "Delete" buttons if the logged-in user has permission for these actions.
All of these elements are already defined in the original template that generated the page, so now we must replicate them using JavaScript!
We can employ another templating language, such as jQuery's Template plugin, to help with creating and inserting this new li block. However, this still results in duplicate HTML markup that may differ slightly because we lack macros or similar features provided by the initial templating language.
How can we eliminate this duplication? Is it feasible, or must we simply accept it? What approaches are considered best practices for resolving this issue?