Skip to content

Instantly share code, notes, and snippets.

@cmilfont
Created July 27, 2013 18:09
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/6095741 to your computer and use it in GitHub Desktop.
Save cmilfont/6095741 to your computer and use it in GitHub Desktop.
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