模块"/>
template模块
掌握template.render(data.toString(),{json:arr})使用
首先需要安装template.js在code里------npm i art-template
传统的模板引擎回忆
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script src="./node_modules/art-template/lib/template-web.js"></script><script type="text/template" id="template">{{each json}}<p>姓名:{{$value.name}}</p><p>年龄:{{$value.age}}</p>{{/each}}</script><script>var arr=[{name:"zhangsan1",age:19},{name:"zhangsan2",age:18},{name:"zhangsan3",age:20},];console.log(typeof(arr));console.log(typeof({arr:arr}));var html=template("template",{json:arr //此处必须是对象形式});console.log(html);</script></body>
</html>
node里使用模板
var template=require('art-template');
var http=require('http');
var fs=require('fs');
var arr=[{name:"zhangsan1",age:19},{name:"zhangsan2",age:20},{name:"zhangsan3",age:18},
]
http.createServer(function(req,res){fs.readFile('./t.html',function(err,data){if(err){return res.end('404');}var html=template.render(data.toString(),{json:arr})res.end(html);})
}).listen(3000,function(){console.log("running...");})
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body>{{each json}}<p>姓名:{{$value.name}}</p><p>年龄:{{$value.age}}</p>{{/each}}
</body>
</html>
更多推荐
template模块
发布评论