模板引擎ejs详解

singsingasong.js:

const ejs=require('ejs');

ejs.renderFile('./views/singsingasong.ejs', {'name':'singsingasong',json: {arr: [
{user: 'blue', pass: '123456'},
{user: 'zhangsan', pass: '654321'},
{user: 'xiaoming', pass: '999999'},
]}}, function (err, data){
console.log(data);
});

singsingasong.ejs :

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge"> -->
<title>zhagnwen's ejs-测试</title>
</head>
<body>
<div>
<%= name %>
<%= json.arr[0].user%>
<%= json.arr[0].pass%>
<%= 13 %> + <%= 4 %> <!-- 等号 是转意输出,减号 不转意输出 -->
<% var str="<div class=‘div1’></div>" %>
<%-str %>
<!-- 引用 a.txt文件 -->
<% include ../a.txt %> <%for(var i=0;i<5;i++){%>
<% include ../a.txt %> //引入外部文件时注意层级‘../’
<% } %>
}
</div>
</body>
</html>

知识点:

如果用到include,必须把它单独标记出来.因为它是ejs自带的,不是js的:

<% if(type=='admin'){ %>
<% include ../style/admin.css %>
<%}else{%>
<% include ../style/user.css %>
<% } %>
上一篇:flask中的return、过滤器详解


下一篇:从Chrome源码看JS Array的实现