Node.JS/Express 视图中 Headers/Footer/Reusable 模板的最佳实践?
Best Practice for Headers/Footer/Reusable Templates in Node.JS/Express Views?
我用 Node.JS / Express / Pug 设置了我的开发环境,我正在学习如何使用视图和路由,但我找不到关于如何包含 "reusable" 导航栏和页脚。
在 PHP 中,您只需包含 header.php 和 footer.php 文件,它会将它们拉入每个页面,在 Node/Express 中执行此操作的最佳方法是什么?
我看到术语 "partial view" 被抛出但与页眉和页脚无关,所以我正在寻找实现此目的的最佳方法。
假设我有 3 个视图:索引、关于和联系我如何在所有 3 个视图中包含相同的导航栏和页脚而不将其复制并粘贴到每个视图中?只需要朝着正确的方向大力推动。
提前致谢
要撰写回复,您有以下几种选择:
1) 让模板引擎完成它的工作:
doctype html
html
include includes/head.pug
文档
2) 使用express一次发送多个文件:
app.use((req, res, next) => {
const header = fs.createReadStream("header.html");
header.pipe(res);
header.on("close", next);
});
app.get("/", (req, res, next) => {
// Send the homepage
});
//....
我用 Node.JS / Express / Pug 设置了我的开发环境,我正在学习如何使用视图和路由,但我找不到关于如何包含 "reusable" 导航栏和页脚。
在 PHP 中,您只需包含 header.php 和 footer.php 文件,它会将它们拉入每个页面,在 Node/Express 中执行此操作的最佳方法是什么?
我看到术语 "partial view" 被抛出但与页眉和页脚无关,所以我正在寻找实现此目的的最佳方法。
假设我有 3 个视图:索引、关于和联系我如何在所有 3 个视图中包含相同的导航栏和页脚而不将其复制并粘贴到每个视图中?只需要朝着正确的方向大力推动。
提前致谢
要撰写回复,您有以下几种选择:
1) 让模板引擎完成它的工作:
doctype html
html
include includes/head.pug
文档
2) 使用express一次发送多个文件:
app.use((req, res, next) => {
const header = fs.createReadStream("header.html");
header.pipe(res);
header.on("close", next);
});
app.get("/", (req, res, next) => {
// Send the homepage
});
//....