ejs模板库
1. 配置安装
2. 基础语法
- ejs中可以插入任何的js
- ejs中输出html(直接将"="变为"-")
- ejs用
1. ejs配置安装:
cnpm install ejs
2. ejs基础语法
ejs可以直接插入在html结构中,.ejs文件内容:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
我的名字叫:<%= name%>
</body>
</html>
在.js文件中:
const ejs = require('ejs');
ejs.renderFile('./views/1.ejs',{},function(err,data){
if(err)
console.log("编译失败");
else
console.log(data);
});
这样直接编译会执行编译出错,因为.ejs文件中的name没有值。将ejs.renderFile改为
ejs.renderFile('./views/1.ejs',{name: 'jsyang'},function(err,data){
if(err)
console.log("编译失败");
else
console.log(data);
});
编译出的结果为:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
我的名字叫:yang
</body>
</html>