[英]How do I write a conditional for express-sessions login/logout in my main handlebars layout?
[英]How do I reuse my layout with handlebars in express?
好吧,我不想在每個視圖中包含我的實際布局。
所以我的觀點看起來像
<html lang="en">
<head></head>
<body>
<div id="content">
{{ content }}
</div>
</body>
</html>
<div>my index content</div>
<div>my help content</div>
當我使用我的快速路線時,如下面的例子,我只需要在每個視圖中包含我的整個布局 - 這顯然是一個壞主意。
app.use('/', function (req, res) {
res.render('index', {content: 'foo'});
});
app.use('/about', function (req, res) {
res.render('about', {content: 'foo'});
});
所以問題是:如何在車把視圖中重用我的布局?
我的想法是另外編譯我的內容
app.use('/', function (req:express.Request, res:express.Response, next:Function) {
fs.readFile(path.join(viewPath, 'index.hbs'), function(err, text){
var template = hbs.handlebars.compile(text);
var content = template({foo: 1234});
res.render('layout', {content: content});
});
});
但我不確定這是不是正確的方法。
您可以使用express-handlebars
並為其指定默認布局。
var hbs = exphbs.create({
extname:'hbs',
layoutsDir: '/path/to/layouts',
defaultLayout: 'main'
});
// Initialize engine
app.engine('hbs', hbs.engine);
// Set engine
app.set('view engine', 'hbs');
設置布局后,您只需調用res.render({content: content});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.