1

有没有办法将视图呈现给变量?我想改变这一点。

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 作为渲染引擎。

4

1 回答 1

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);
});
于 2015-01-04T17:23:12.547 回答