调用sails.renderView时,在Sails.js中指定布局文件

我使用sails.renderView帮助器方法sails.renderView将视图渲染成随后在电子邮件中使用的string,如下所示:

 sails.renderView('emails/booking', emailData, function (err, view) { emailService.send(user.email, view); }); 

以这种方式渲染视图时是否可以指定布局文件? 我使用ejs模板引擎。

它看起来不像我可以使用视图configuration( config/views.js ),因为它的基于路线。

我看了sails/lib/hooks/views/render.js中的渲染函数,它看起来像要使用的布局文件可以在选项对象中传递,但是当我这样做,没有视图返回。

编辑:对不起,我错过了这是正常生成的请求/响应上下文。 答案改变反映了。

我通过了帆码,特别是渲染引擎

 /** * sails.hooks.views.render(relPathToView, options, cb_view) * * @param {String} relPathToView * -> path to the view file to load (minus the file extension) * relative to the app's `views` directory * @param {Object} options * -> options hash to pass to template renderer, including locals and locale * @param {Function} cb_view(err) * -> called when the view rendering is complete (response is already sent, or in the process) * (probably should be @api private) * @api public */ ... lots of code ... // if local `layout` is set to true or unspecified // fall back to global config var layout = options.layout; if (layout === undefined || layout === true) { layout = sails.config.views.layout; } 

事实certificate,您可以将布局设置为您传递给sails.renderView()的选项对象的一部分

所以,如果你有一个电子邮件模板emailLayout.ejs ,你可以通过添加这行到你的控制器来使用它:

 emailData.layout = '/emailLayout'; sails.renderView('emails/booking', emailData, function (err, view) { emailService.send(user.email, view); }); 

大警告,这显然只适用于ejs模板。 我没有testing过,所以让我知道如何去