public
Created

  • Download Gist
prototype.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
function Engine(template) {
var pattern = /\#\{([^}]+)\}/g;
var _render = function(json){
return template.replace(pattern, function(m, value) {
return json[value];
});
};
this._r = function() {
return _render.apply(this, arguments);
}
};
Engine.prototype = {
render: function(json) { return this._r(json) }
};
var json = { title: "Cadastro", url: "/cursos", desc: "cliqui aqui" };
var template = "<h1>#{title}</h1> " +
"<a href='#{url}'>#{desc}</a>";
new Engine(template).render(json);
replace.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Object.defineProperty(String.prototype, 'interpolate', {
value: function(json){
var pattern = /\#\{([^}]+)\}/g;
this.replace(pattern, function() {
console.log(arguments);
return "";
});
}
});
 
var json = { title: "Cadastro", url: "/cursos", desc: "cliqui aqui" };
var template = "<h1>#{json.title}</h1> " +
"<a href='#{json.url}'>#{json.desc}</a>";
$("#nav").html( template.interpolate(json) );
singleton.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var Engine = (function(){
 
var pattern = /\#\{([^}]+)\}/g;
var _render = function(template, json){
return template.replace(pattern, function(m, value) {
return json[value];
});
};
 
return {
render: _render
};
 
})();
 
var json = { title: "Cadastro", url: "/cursos", desc: "cliqui aqui" };
var template = "<h1>#{title}</h1> " +
"<a href='#{url}'>#{desc}</a>";
Engine.render(template, json)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.