Created
January 25, 2014 13:09
-
-
Save geminiwen/8616142 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
var fs = require("fs"); | |
var fd = fs.openSync("template.html","r"); | |
fs.readFile("template.html", {encoding:"utf-8"},function(err,data){ | |
if(err) { | |
console.err("error in open file with:" + err); | |
return; | |
} | |
data = data.replace(/\n/g, '\\n') | |
.replace(/<%=([\s\S]+?)%>/,function(match,code){ | |
return "' + " + code + " + '"; | |
}); | |
var tpl = "tpl ='" + data +"'"; | |
tpl = 'var tpl="";\nwith(obj){ ' + tpl + ' }\nreturn tpl;'; | |
var compile = new Function('obj',tpl); | |
console.log(compile({name:"GeminiWen"})); | |
}); | |
/*** | |
* | |
* output: | |
*<!DOCType html> | |
*<html> | |
* <head> | |
* <meta charset="utf-8" /> | |
* <title>我是来测试的</title> | |
* </head> | |
* <body> | |
* Hello GeminiWen | |
* </body> | |
*</html> | |
* | |
***/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment