使用 hapi 和 handlebars,以及 hapi 的默认布局支持,是否可以从页面中选择替代布局?
Using hapi and handlebars, with the default layout support from hapi, can alternative layout be selected from with a page?
Another answer 和 hapi api 表示 hapi 在使用手柄栏时内置了对布局的支持。然而,似乎只允许在配置中定义一种布局作为默认 'layout.html' 布局的替代方案。
在 that answer 中展示了如何使用 handlebars-layouts 来使用 handlebars 布局支持在页面中执行此操作,如下所示:
{{#extend "layout2"}}{{/extend}}
虽然我可以使用车把布局,但我只想使用 hapi 提供的尽可能多的内置内容。
是否可以在页面模板中使用默认布局和 select 布局以外的布局?可能是这样的:
{{!< layout/layout2}}
您可以覆盖 reply.view()
中的视图管理器配置:
options
- optional object used to override the server's views manager configuration for this response. Cannot override isCached
, partialsPath
, or helpersPath
which are only loaded at initialization.
这是一个例子:
index.js:
var Handlebars = require('handlebars');
var Hapi = require('hapi');
var Path = require('path');
var server = new Hapi.Server()
server.connection({
host: '127.0.0.1',
port: 8000
});
server.views({
engines: {
html: Handlebars.create()
},
path: Path.join(__dirname, 'views'),
layoutPath: Path.join(__dirname, 'views/layouts'),
layout: 'default'
});
server.route({
method: 'GET',
path: '/default',
handler: function (request, reply) {
reply.view('item', { title: 'Item Title', body: 'Item Body' });
}
});
server.route({
method: 'GET',
path: '/custom',
handler: function (request, reply) {
reply.view('item', { title: 'Item Title', body: 'Item Body' }, { layout: 'custom' });
}
});
server.start();
views/layouts/custom.html:
<html>
<body>
<h1>Custom Layout</h1>
{{{content}}}
</body>
</html>
views/layouts/default.html:
<html>
<body>
<h1>Default Layout</h1>
{{{content}}}
</body>
</html>
views/item.html:
{{body}}
当您访问 http://localhost:8000/default, it will use default.html
. However http://localhost:8000/custom 时将使用 custom.html
。
Another answer 和 hapi api 表示 hapi 在使用手柄栏时内置了对布局的支持。然而,似乎只允许在配置中定义一种布局作为默认 'layout.html' 布局的替代方案。
在 that answer 中展示了如何使用 handlebars-layouts 来使用 handlebars 布局支持在页面中执行此操作,如下所示:
{{#extend "layout2"}}{{/extend}}
虽然我可以使用车把布局,但我只想使用 hapi 提供的尽可能多的内置内容。
是否可以在页面模板中使用默认布局和 select 布局以外的布局?可能是这样的:
{{!< layout/layout2}}
您可以覆盖 reply.view()
中的视图管理器配置:
options
- optional object used to override the server's views manager configuration for this response. Cannot overrideisCached
,partialsPath
, orhelpersPath
which are only loaded at initialization.
这是一个例子:
index.js:
var Handlebars = require('handlebars');
var Hapi = require('hapi');
var Path = require('path');
var server = new Hapi.Server()
server.connection({
host: '127.0.0.1',
port: 8000
});
server.views({
engines: {
html: Handlebars.create()
},
path: Path.join(__dirname, 'views'),
layoutPath: Path.join(__dirname, 'views/layouts'),
layout: 'default'
});
server.route({
method: 'GET',
path: '/default',
handler: function (request, reply) {
reply.view('item', { title: 'Item Title', body: 'Item Body' });
}
});
server.route({
method: 'GET',
path: '/custom',
handler: function (request, reply) {
reply.view('item', { title: 'Item Title', body: 'Item Body' }, { layout: 'custom' });
}
});
server.start();
views/layouts/custom.html:
<html>
<body>
<h1>Custom Layout</h1>
{{{content}}}
</body>
</html>
views/layouts/default.html:
<html>
<body>
<h1>Default Layout</h1>
{{{content}}}
</body>
</html>
views/item.html:
{{body}}
当您访问 http://localhost:8000/default, it will use default.html
. However http://localhost:8000/custom 时将使用 custom.html
。