I make a call to my api and returns json objects, then it generates a lot of divs full of content. But i keep using concatenation to insert my object properties. Is there a better way to to that?
$.each(JSON, function(key, value) {
var content = display_mention(value);
$("#mentions_container").append(content);
});
function display_mention(mention) {
//this str will be much more complex and use lots of concatenation
var str = "<div data-id='" + mention.id +"'> " + mention.texto + "</div></br>";
return str;
}
There is a better way and it’s called client side templating.
Look at mustache.js https://github.com/janl/mustache.js
or handlebars.js http://handlebarsjs.com/