Skip to content

Instantly share code, notes, and snippets.

@geminiwen
Created January 25, 2014 13:09
Show Gist options
  • Save geminiwen/8616142 to your computer and use it in GitHub Desktop.
Save geminiwen/8616142 to your computer and use it in GitHub Desktop.
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