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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:49:59+00:00 2026-05-28T20:49:59+00:00

It might be easiest to explain the question with an example. I have a

  • 0

It might be easiest to explain the question with an example.

I have a handlebars template defined in an HTML page:

   <script id="myTemplate" type="text/html">
      <h1>{{fieldname}}</h1>
   </script>

I render the template with handlebars:

   var source = $("#myTemplate").html();
   var template = Handlebars.compile(source);
   var txt = template(jsonObj);

and txt ends up containing the rendered html text. No problem.

BUT…

say I’ve retrieved an XML file via ajax, and I’d like to use jquery to reach into it to render elements.

In order to retrieve, say, the value of the “name” child element from a parent element, I might use jquery like this:

   $(xml).find('name').text()

So, I thought my template might look something like this: (NOTE THIS DOES NOT WORK!)

   <script id="myTemplate" type="text/html">
      <h1>{{$(this).find('name').text()}}</h1>
   </script>

EDIT: Obviously, this doesn’t work.

I also tried

{{this.find('name').text()}}

and even

{{.find('name').text()}}

But none of these work, and so far I’ve been unable to find any of the JS templating systems where you can do something like this. Handlebars and DUST seem the most likely to be able to do it, but I haven’t had any luck thus far.

I’ve seen some indication that I could convert the XML to JSON and just pass that as the context, and that may indeed be the only way.

Is it easier than I’m making it out to be? Impossible?

EDIT:
After more digging, it looks like there’s just not a templating solution for Javascript out there that works interchangeably with JSON or XML Dom objects for the data context. All of them appear to assume json.

Soooo… I used one of the many jquery plugins available to convert xml2json, and pass THAT object into HandleBars and everything works swimmingly.

The only trick that I ran into is if you perform a JQUERY query on the XML data to find a particular element, what you get back is an ARRAY of XML dom objects, even if there’s only 1 result.

So, I had to do something like:

var txt = template($.xml2json($(xml).find('myelementname')[0]));

And all is good.

I may open another question about whether anyone knows of a Javascript template engine that takes either json OR xml dom objects.

  • 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-28T20:49:59+00:00Added an answer on May 28, 2026 at 8:49 pm

    You cannot do that because the template parser do not know what this is. In the template you can only only play with the data you provide to it.

    However you can take a look at Underscore templates of doT js they might be helpful to you.

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

Sidebar

Related Questions

I think the easiest way to explain this is by example. I have a
This might be a basic CSS question: I have a site designed with well-defined
This might be the easiest question of the day. i have a groups of
this might be the easiest problem to solve but I have searched through the
This might seem like a stupid question I admit. But I'm in a small
You might have a set of properties that is used on the developer machine,
This might be an interesting question. I need to test that if I can
This might sound like a little bit of a crazy question, but how can
This might be an odd question, but when I scale my image in C#
What's the easiest way to quickly create some simple HTML in Python? All I've

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.