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.
-
3Use a templating engine, such as Knockout.jscgatian– cgatian2014-06-12 23:29:44 +00:00Commented Jun 12, 2014 at 23:29
-
@cgatian How would these work (not the library itself but the concept)? Thank you!user3704920– user37049202014-06-12 23:31:28 +00:00Commented 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.comLukas S.– Lukas S.2014-06-12 23:37:52 +00:00Commented Jun 12, 2014 at 23:37
-
Some good ideas here: stackoverflow.com/questions/6001904/…Andrew– Andrew2014-06-12 23:44:55 +00:00Commented Jun 12, 2014 at 23:44
Add a comment
|
2 Answers
Templating tools such as handlebars, mustache and dust.js were designed to solve this very problem.
4 Comments
sahbeewah
@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.
Lukas S.
@RokoC.Buljan, I would say from the sound of the original question, the user is not aware that these libraries even exist.
Andrew
Might want to look at angular.js as well. angularjs.org
Roko C. Buljan
@Andrew +1 even better. <3
ng