我如何重复我的布局与快递车把?
好的,我不想在每个视图中包含我的实际布局。
所以我的意见看起来像
layout.hbs
<html lang="en"> <head></head> <body> <div id="content"> {{ content }} </div> </body> </html>
index.hbs
<div>my index content</div>
help.hbs
<div>my help content</div>
当我使用我的快速路线,如下面的例子,我只需要在每个视图中包括我的整个布局的东西 – 这显然是一个坏主意。
expression路线
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});