Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 调用sails.renderView时在Sails.js中指定布局文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用sails helper方法sails.renderView将视图呈现为一个字符串,然后在电子邮件中使用,如下所示:

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

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

它看起来不像我可以使用视图配置(config / views.js),因为它是基于路由的.

我查看了sails / lib / hooks / views / render.js中的render函数,看起来要使用的布局文件可以在options对象中传递,但是当我这样做时,没有返回任何视图.

解决方法

编辑:对不起我错过了这是在常规请求/响应上下文之外生成的.答案改为反映这一点.

我通过风帆代码,特别是the render engine

/**
 * 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;
  }

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

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

emailData.layout = '/emailLayout';
sails.renderView('emails/booking',view);
});

大警告,这显然只适用于ejs模板.我没有测试过这个,所以让我知道它是怎么回事

大佬总结

以上是大佬教程为你收集整理的node.js – 调用sails.renderView时在Sails.js中指定布局文件全部内容,希望文章能够帮你解决node.js – 调用sails.renderView时在Sails.js中指定布局文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: