如何使用车把更改Express中的默认布局? [英] How to change default layout in express using handlebars?
问题描述
我正在使用Express 4.9.0和express-generator.
I am using Express 4.9.0 and express-generator.
使用以下命令创建样板:
Created boilerplate with a following command:
express --hbs projectname
默认情况下,Builtin车把使用views/layout.hbs
作为主页.但是我无法在我的app.js中看到任何设置来更改该行为.
Builtin handlebars is using views/layout.hbs
by default as a master page. But i cannot see any settings in my app.js to change that behaviour.
来自我的app.js的代码片段:
piece of code from my app.js:
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
- 如何全局更改默认布局?
- 如果我想要2或3种不同的全局布局怎么办?
推荐答案
您可以指定要在渲染调用中使用的布局.如果创建名为other.hbs
的新布局,则可以执行以下操作:
You can specify what layout you want to use as part of the render call. If you create a new layout called other.hbs
, you can then do something like:
res.render('view', { title: 'my other page', layout: 'other' });
要在整个应用程序中覆盖它,可以使用:
To override this for the entire application, you can use:
app.set('view options', { layout: 'other' });
这篇关于如何使用车把更改Express中的默认布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!