1

For my project I am starting to get alot of embeded html in my javascript as a result of ajax calls. I want to move them out of there. What is the best practice for this? I was thinking have the elements as a div in the page with display:none then deal with when the javascript needs it.

But If I use this method I need to replace certain string with the data return from my ajax call, any advise on that?

Thanks

charleetm
  • 916
  • 1
  • 12
  • 26
  • what i usually do is add formatting definitions in the returned json data. this way the correct html is automatically generated – Ibu Jun 19 '11 at 22:28
  • @Ibu, JSON is for data transport, adding formatting to it kind of makes it like HTML, so if you are doing this why aren't you using HTML directly? – Darin Dimitrov Jun 19 '11 at 22:32

1 Answers1

3

You could use a templating framework such as jquery templates. Here's a blog post about it.

The are also other engines you might take a look at:

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I like MicroTemplating best, but use the jQuote2 version of it, not the original. http://aefxx.com/jquery-plugins/jqote2/ – Ariel Jun 19 '11 at 23:31
  • If starting with templates, take a look at this question: http://stackoverflow.com/questions/170168/jquery-templating-engines – Anton Strogonoff Jun 20 '11 at 02:31