Created
July 27, 2013 18:09
-
-
Save cmilfont/6095741 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 Eljs(template){ | |
var chaves = {}; | |
var pattern = /\#\{([^}]+)\}/g; | |
var pattern_compiled = /\$\{([^}]+)\}/g; | |
var compiled = ""; | |
(function(engine){ | |
compiled = template.replace(pattern_compiled, function(p, valor, index){ | |
chaves[index] = new Function("json", "return json." + valor + ";"); | |
console.log( chaves[index] ); | |
return "#{" + index + "}"; | |
}) | |
})(this); | |
this._parse = function(json){ | |
return compiled.replace(pattern, function(p, valor, index){ | |
return chaves[valor](json); | |
}) | |
} | |
console.log(chaves); | |
} | |
Eljs.prototype = { | |
parse: function(json){ | |
return this._parse(json); | |
} | |
} | |
var template = "<span>O nome completo do paciente é: ${getNome()}</span> com email ${user.email}" | |
var engine = new Eljs(template); | |
engine.parse( { getNome: function(){ return this.user.nome; }, user: { email: "cmilfont@gmail.com", nome: "Paulo" } } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment