Created
September 17, 2011 18:43
-
-
Save chicojunior/1224218 to your computer and use it in GitHub Desktop.
Curso_Javascript_Fundamental
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 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