我使用的是安装了 Express-Framework 的 Node.js 服务器,但在组合两个不同的 html 文件时遇到问题。
我有一个基本的 template.html 文件,其中包含 <div id="content">
在里面。现在我想在将数据发送到客户端之前在此 div 中包含 content.html。
如何做到这一点?
最佳答案
我喜欢使用 EJS,因为它是最直接的模板语言,但同样的想法也适用于 jade 或任何您可能使用的东西。要在运行 npm install ejs
后在 Express 中启用此功能:
app.engine('.ejs', require('ejs').__express);
当您发送回复时,运行:
res.render('content.html.ejs');
将 template.html 放入 views/header.html.ejs
和 views/footer.html.ejs
中,并将它们包含在 views/content.html.ejs
中,如下所示:
<% include header.ejs %>
Content Here
<% include footer.ejs %>
关于node.js - 在 Node.js Express 上渲染 HTML(在另一个 HTML 文件中包含 HTML 代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16213855/