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 7588857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:01:50+00:00 2026-05-30T20:01:50+00:00

In an attempt to avoid repetition, I am trying to use mustache for templating

  • 0

In an attempt to avoid repetition, I am trying to use mustache for templating on both the server and client side. I have a template something like the following:

<html>
<head>...</head>
<body>
  <table>
    <tbody id="#this">
      {{rows}}
      <tr>{{row}}<tr>
      {{/rows}}
    </tbody>
  </table>
</body>
</html>

This works great for my server side templating. On the client side however, I’m having some trouble. I can successfully load the template using ajax, but I don’t need the whole thing to update the rows of the table.

$(function () {
    var template;
    $.ajax('templates/thetemplate.mustache', {
        success : function (data) {
            template = $('#this', data).html();
        },
        dataType : 'html'
    });
});

When I use the above jQuery to get at the contents of the #this element however, the {{rows}} and {{/rows}} lines are stripped from the result, while the <tr>{{row}}</tr> between them are returned successfully. How can I get the entire contents?

I’ve tried $('#this', data).contents(); which gives the same results, and $('#this', data).val(); which returns an empty string. Using $(data).find('#this') instead has the same three results. I’ve also tried setting the dataType of the ajax call to 'text' with no apparent effect.

I realize that I could probably accomplish what I want (not duplicating parts of my templates for client vs server side use) by using partials, and that it would have the added bonus of avoiding the transfer of more template than I actually need to the client just to have it thrown away, but that doesn’t answer my question. (tho slick solutions are appreciated)

Thanks

  • 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-30T20:01:52+00:00Added an answer on May 30, 2026 at 8:01 pm

    The problem has already occurred at $('#this', data), which sends data via the browser’s HTML parser to convert it to a DOM fragment. Being error tolerant, the HTML parser does what it can with the template but it’s not what you want because the template itself is not valid HTML. For this reason $('#this', data).anythingYouLike() or $(data).anythingYouLike() will not work.

    As the HTML must be valid before submitting to the browser’s HTML parser, there’s no other choice than to perform Mustache rendering before submitting valid HTML to the browser with a $(renderedHTML) expression.

    With that constraint in mind, there are two realistic options regarding the order of events :

    • strip down data to just <tbody ...>...</tbody> (with
      a regular expression), then Mustache render, then $tbody = $(renderedHTML).
    • Mustache render data in full, then $tbody = $(renderedHTML).find('tbody')

    As far as I’m aware, either approach will work. In both cases, you end up with a jQuery object, $tbody, containing a populated tbody node, which can then be inserted into the DOM with $tbody.appendTo($('#myTable')) or similar.

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

Sidebar

Related Questions

This is my first attempt to use boost::threads and I have a silly question.
First attempt to use this cool site - after searching for 2 hours: So
I attempt to use webservice return POCO class generated from entity data model as
In an attempt to wrap some unmanaged code in a managed .dll I'm trying
I'm attempt to call a C++ dll with a struct and function like struct
in my attempt to learn a bit faster the use of spring and hibernate
In an attempt to learn to use PInvoke in C#, I'm a little unsure
To avoid having to repeat type parameter information when subclassing parameterized classes, I'm trying
I am trying to avoid hard-coding each instance of this WYSIWYG editor so I
I currently have some code I am trying to refactor. A large set of

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.