-Effective JavaScript templating
安装npm
npm install ejs
渲染HTMLrender()
var ejs = require('ejs'),
people = ['geddy', 'neil', 'alex'],
html = ejs.render('<%= people.join(", "); %>', {people: people});
标签<% %>
- <% js代码,控制流,不输出
- <%= 原样输出
- <%- 输出(HTML会被解析)
- <%# 注释,不执行,不输出
- <%% 输出 '<%'
- %> 结束标签
- -%> 换行删除空格
自定义标签delimiter
var ejs = require('ejs'),
users = ['geddy', 'neil', 'alex'];
ejs.render('<?= users.join(" | "); ?>', {users: users},
{delimiter: '?'});
ejs.delimiter = '$';
ejs.render('<$= users.join(" | "); $>', {users: users});
引入文件include
<ul>
<% users.forEach(function(user){ %>
<%- include('user/show', {user: user}); %>
<% }); %>
</ul>