标签(空格分隔): ejs
//设置模板目录
app.set(‘views‘, path.join(__dirname, ‘views‘));
//设置模板引擎
app.set(‘view engine‘, ‘html‘);
//设置引擎后缀. index.html 中的内容可以是 ejs 代码
app.engine(‘.html‘, require(‘ejs‘).__express);
ejs的特性: 1、缓存功能,能够缓存已经解析好的html模版; 2、<% code %>用于执行其中javascript代码; 3、<%= code %>会对code进行html转义; 4、<%- code %>将不会进行转义; 5、支持自定义标签,比如’<%‘可以使用’{{’,’%>‘用’}}‘代替; 6、提供一些辅助函数,用于模版中使用 7、利用<%- include filename %>加载其他页面模版;
<% include layout.html%> <%- include layout.html%>
//设置闭合标签
var ejs = require(‘ejs‘);
ejs.open = ‘{{‘;
ejs.close = ‘}}‘;
express中全局设定
app.set("view options",{
"open":"{{",
"close":"}}"
});
//传递 index 模板引擎参数
res.render(‘index‘,{‘title‘:‘haha‘);
//使用参数
<title><%= title %></title>
<%for(var i=p.length-1; i>=0; i--){%>
<input type="button" value=<%=p[i]%>>
<%}%>
辅助函数使用
//注意闭合标签 <%=: %>
<input type="button" value=<%=:p|first%>>
app.locals[‘say’] = function(){ return ‘hello’; }; 在 app.locals 中定义的方法都可以在模板中引用 <input type=“button” value=<%=say()%>>
原文:http://www.cnblogs.com/wuxiang/p/5227457.html