您可以覆盖reply.view() 中的视图管理器配置:
options - 可选对象,用于覆盖此响应的服务器视图管理器配置。无法覆盖仅在初始化时加载的 isCached、partialsPath 或 helpersPath。
这是一个例子:
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();
视图/布局/custom.html:
<html>
<body>
<h1>Custom Layout</h1>
{{{content}}}
</body>
</html>
视图/布局/default.html:
<html>
<body>
<h1>Default Layout</h1>
{{{content}}}
</body>
</html>
视图/item.html:
{{body}}
当您访问http://localhost:8000/default 时,它将使用default.html。但是http://localhost:8000/custom 将使用custom.html。