Skip to content

Instantly share code, notes, and snippets.

@chicojunior
Created September 17, 2011 18:43
Show Gist options
  • Save chicojunior/1224218 to your computer and use it in GitHub Desktop.
Save chicojunior/1224218 to your computer and use it in GitHub Desktop.
Curso_Javascript_Fundamental
var objeto = {
nome: "Júnior Santos", telefone: "4567545676"
};
var template = "<div>#{nome} \ - #{telefone} - #{ (idade >=18)? \"permitido": \"proibido\"},<div>";
function Engine(tmpl){
var pattern = /\#\{([^}]+)\}/g;
var _json = {};
var _parse = function(expression){
console.log(expression);
var corpo = "";
for(var propriedade in _json){
corpo += "var " + propriedade + " =\"" +_json[propriedade] + "\";";
}
};
this.parse = function(json){
return tmpl.replace(pattern, function(match, value){
var result = json[value];
return (result)? result: "";
});
};
}
String.prototype.interpolate = function(json){
var pattern = /\#\{([^}]+)\}/g;
return this.replace(pattern, function(match, value){
var result = json[value];
return(result)? result: "";
});
};
var html = new Engine(template).parse(json);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment