Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3608748
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:32:24+00:00 2026-05-18T21:32:24+00:00

I consistently come across this code smell where I am duplicating markup, and I’m

  • 0

I consistently come across this code smell where I am duplicating markup, and I’m not really sure how to fix it. Here’s a typical use case scenario:

Let’s say we’d like to post comments to some kind of article. Underneath the article, we see a bunch of comments. These are added with the original page request and are generated by the templating engine (Freemarker in my case, but it can be PHP or whatever).

Now, whenever a user adds a comment, we want to create a new li element and inject it in the current page’s list of comments. Let’s say this li contains a bunch of stuff like:

  1. The user’s avatar
  2. Their name
  3. A link to click to their profile or send them a private message
  4. The text they wrote
  5. The date they wrote the comment
  6. Some “edit” and “delete” links/buttons if the currently logged in user has permission to do these actions.

Now, all of these things were already written in our template that originally generated the page… so now we have to duplicate it inside of Javascript!

Sure, we can use another templating language – like Jquery’s Template plugin – to ease the pain generating and appending this new li block… but we still end up with duplicate html markup that is slightly different because we can’t use macros or other conveniences provided to us by the templating language.

So how do we refactor out the duplication? Is it even possible, or do we just put up with it? What are the best practices being used to solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:32:25+00:00Added an answer on May 18, 2026 at 9:32 pm

    This is a common problem and becomes more obvious as the UI complexity increases, and changes have to be done on both the server and client templates. This problem is fixable by using a the same template markup on both the client and server sides. The template processors must be written in both JavaScript and the server side language.

    Two other solutions that are cleaner than the above approach, but both have their own problems:

    • Do everything client side
    • Do everything server side

    If all markup generation is done on the client side, then the server acts more or less like a web service which only sends back data in whatever formats suits the application. JSON, and XML are really popular formats for most web services nowadays. The client always generates the necessary HTML and JS. If going with this approach, the boundary between the client and server must be well defined. Since the client has limited knowledge of what happens on the server, this means that proper error codes must be defined. State management will become harder since most/all server interaction will be happening asynchronously. An example of adding a comment with this approach may look like:

    $('#add-comment').click(function() {
        var comment = $('#comment-box').text();
        $.ajax('http://example.com/add', {
            success: function() {
                addCommentRow(comment);
            },
            ...
        });
    });
    
    function addCommentRow(comment) {
        var user = currentUser().name;
    
        var html = "<li><b>{user}</b> says {comment}</li>";
        html = html.replace("{user}", user).replace("{comment}", comment);
    
        var item = $('<li>').html(html);
        $('#comments').append(item);
    }
    

    The other approach is to do everything server side. Whenever a change happens, shoot a request to the server, and ask it for the updated view. With a fast backend, response times under a second, and proper indicators of network activity, the application should seem very responsive despite everything happening on the server. The above example would be simplified to:

    $('#add-comment').click(function() {
        $.ajax('http://example.com/add', {
            success: function(response) {
                $('#comments').html(response);
            },
            ...
        });
    });
    

    Although this seems a lot more cleaner on the client side than the previous approach, we have just moved the markup generation up to the server. However, if the application is not very AJAXy like Google Maps, then this approach may be easier to work with. Again, it’s a matter of how complicated the application is, and perhaps maintaining state client side is a necessity for you, in which case you may want to go with the previous approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've consistently had an issue with parsing XML with PHP and not really found
I encounter this issue pretty consistently when trying to merge a branch back into
I have some existing code which isn't formatted consistently -- sometimes two spaces are
On several other databases I've come across, one can turn off the consistency check
BASE stands for 'Basically Available, Soft state, Eventually consistent' So, I've come this far:
I have the code like below to get 'content' width, but how come it
I am designing a contact management system and have come across an interesting issue
In Perl before 5.10 there is no state declaration. I've come across an example
This is a question about LINQ the .NET language feature, not LINQ to SQL.
Has anyone come across a web-safe colour algorithm? Perhaps to explain why I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.