test.ejs

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<% for(var i=;i<json.arr.length;i++){ %>
<div>用户名:<%=json.arr[i].user%> 密码:<%=json.arr[i].pass%></div>
<% } %>
</body>
</html>

ejs.js

 ejs=require('ejs');

ejs.renderFile('./views/test.ejs', {json: {arr: [
{user: '李四', pass: ''},
{user: '张三', pass: ''},
{user: '丽华', pass: ''},
]}}, function (err, data){
console.log(data);
});

输出效果:

nodejs 模板引擎ejs的简单使用(2)-LMLPHP

05-11 22:34