Skip to content

Instantly share code, notes, and snippets.

@cmilfont
Created November 7, 2012 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmilfont/4035313 to your computer and use it in GitHub Desktop.
Save cmilfont/4035313 to your computer and use it in GitHub Desktop.
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);
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) );
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment