Simple javascript string substitution method
My simple and powerful string substitution method like python’s %s and django’s template language.
var mystring = "Hello {{ name }} {{ surname }}";
mystring.render({
"name": "John",
"surname": "Doe",
});
// "Hello John Doe"
To use this method add underscore.js and this short code:
String.prototype.render = function(obj) {
return _.reduce(_.keys(obj),function(t,k){return t.replace(new RegExp("\{\{(\ *)"+k+"(\ *)\}\}","g"),obj[k])},this.toString());
};
You don’t need to use ugly string concatenation anymore


