6

I have a lot of elements that are created on the fly from data received on the server, as JSON. To create these elements a lot of HTML is going inside my JavaScript code. Is there any solution to this problem? The JavaScript code is polluted and writing HTML tags becomes harder and less maintainable.

4
  • 3
    Use a templating engine, such as Knockout.js Commented Jun 12, 2014 at 23:29
  • @cgatian How would these work (not the library itself but the concept)? Thank you! Commented Jun 12, 2014 at 23:31
  • 1
    @user3704920, run through the first 3 tutorials here: http://learn.knockoutjs.com. It's aimed at Knockout, but I think it will also give you a pretty good idea of what all these so-called JavaScript MV* frameworks are all about. And for a good comparison of what other frameworks are out there, check out this site: todomvc.com Commented Jun 12, 2014 at 23:37
  • Some good ideas here: stackoverflow.com/questions/6001904/… Commented Jun 12, 2014 at 23:44

2 Answers 2

6

Templating tools such as handlebars, mustache and dust.js were designed to solve this very problem.

Sign up to request clarification or add additional context in comments.

4 Comments

@RokoC.Buljan Sure isn't. Which is why my answer doesn't suggest any particular library, but rather states that the solution is to use a templating library which has been tried and tested to solve this issue.
@RokoC.Buljan, I would say from the sound of the original question, the user is not aware that these libraries even exist.
Might want to look at angular.js as well. angularjs.org
@Andrew +1 even better. <3 ng
0

Ember.js is another alternative that allows you to remove HTML from your JavaScript through their templating tool called HTMLBars.

Check it out!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.