Nodejs包含其他视图?
答
如果你不想使用EJS或玉石等,那么你可以用jQuery做。将此代码放在index.html中
<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(function(){
$("#header").load("header.html");
$("#footer").load("footer.html");
});
</script>
</head>
<body>
<div id="header"></div>
<!--Remaining section-->
<div id="footer"></div>
</body>
</html>
答
我找到了解决方案。 server.js
var hbs = require('hbs');
app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
hbs.registerPartials(__dirname + '/views/'); <-------- include folder
的index.html 的index.html包括head.html这样的:
{{> head}}
index
</body>
</html>
答
你所寻找的是一个模板引擎,因为你提到的{{ }}
标签我假设你正在使用Hogan.js
aka mustache(javascript版本)。
该文档可以找到here和你正在寻找的是partials部分。
请注意,默认的快速应用程序(如果您选择hogan)随hjs模块安装,不支持partials,您需要安装hogan-express模块并将其替换。
的部分看起来像这样:
{{> head}}
index
</body>
</html>
局部模板是从发送GET或POST的对象,像这样:
res.render('index.html', {
partials: {
head: 'partials/head.html'
}
});