【发布时间】:2020-10-10 21:42:40
【问题描述】:
我目前正在尝试使用快速把手在我的网站中实施部分。我的代码如下所示:
index.js
const path = require("path");
const express = require("express");
const expressHandlebars = require("express-handlebars");
const app = express();
app.engine("handlebars", expressHandlebars({
defaultLayout: "main",
helpers: {
section: (name, options) => {
if (!this._sections) {
this._sections = {};
}
this._sections[name] = options.fn(this);
return null;
}
}
}));
app.set("view engine", "handlebars");
app.use(express.static("public"));
app.get("/", (req, res) => {
res.render("home", { title: "Home" });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
视图/布局/main.handlebars
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{{ title }}}</title>
{{{_sections.head}}}
</head>
<body>
{{{ body }}}
</body>
</html>
views/home.handlebars
{{#section "head"}}
<style>
h1 {
color: "red";
}
</style>
{{/section}}
<h1>test</h1>
预期结果:
它正确显示带有文本“test”的 h1 标签。但是标题应该是红色的,因为{{#section "head"}},却是黑色的。
我已经正确安装了 express 和 expressHandlebars。
可能出了什么问题?
【问题讨论】:
标签: node.js express handlebars.js express-handlebars