Created
November 7, 2012 23:22
-
-
Save cmilfont/4035313 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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