template模块

编程入门 行业动态 更新时间:2024-10-28 19:33:44

template<a href=https://www.elefans.com/category/jswz/34/1771428.html style=模块"/>

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模块

本文发布于:2023-07-28 18:26:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1274321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   template

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!