有没有办法将视图呈现给变量?我想改变它。
var events = {
lastIndex: 32,
payload = [
{
id: 5,
title: "Hello world!"
conyent: "Lorem ipsum"
},
{
id: 7,
title: "Hello world!"
conyent: "Lorem ipsum"
},
...
{
id: 32,
title: "Hello world!"
conyent: "Lorem ipsum"
}
]
};
return reply.view('event/list', {
events: events.payload
});
对此:
return {
events.lastIndex: 32,
view: toView('event/list',events.payload)
}
我正在使用Hapi v8和Jade作为渲染引擎。
答案 0 :(得分:3)
您可以使用server.render()
。文档中的示例:
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({ port: 80 });
server.views({
engines: { html: require('handlebars') },
path: __dirname + '/templates'
});
var context = {
title: 'Views Example',
message: 'Hello, World'
};
server.render('hello', context, function (err, rendered, config) {
console.log(rendered);
});