节点EJS包附带一个位于./node_modules/ejs/ejs.js或的客户端javascript库./node_modules/ejs/ejs.min.js.在页面上包含此内容后,您将需要加载模板,然后从模板生成HTML.
检测未定义的对象属性
的JavaScript样本(加载页面加载的模板将是一个有点较为理想):
function getData() {
// Grab the template
$.get('/results.ejs', function (template) {
// Compile the EJS template.
var func = ejs.compile(template);
// Grab the data
$.get('/data', function (data) {
// Generate the html from the given data.
var html = func(data);
$('#divResults').html(html);
});
});
}
Run Code Online (Sandbox Code Playgroud)
EJS:
<table>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<% data.forEach(function (d) { %>
<tr>
<td><%- d.id %></td>
<td><%- d.name %></td>
</tr>
<% }); %>
</table>
Run Code Online (Sandbox Code Playgroud)
Ajax调用快递:
app.get('/data', function (req, res) {
res.send({ data: [
{ id: 5, name: 'Bill' },
{ id: 1, name: 'Bob' }
]});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8780 次 |
| 最近记录: |