Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.