Specifying a Layout File in Sails.js When Calling Sails.renderView

I use the sails helper method sails.renderViewto render the view in a string, which is then used in email, for example:

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

Is it possible to specify a layout file when rendering a view this way? I use the ejs template engine.

It doesn't look like I can use the view ( config/views.js) configuration as a route.

I looked at the render function in sails/lib/hooks/views/render.js, and it looked like the layout file to be used could be passed in the options object, but when I did this, the return would not be displayed.

+4
source share
1 answer

EDIT: , , /. , .

,

/**
 * 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 `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', emailData, function (err, view) {
    emailService.send(user.email, view);
});

, ejs. , , .

+3

Source: https://habr.com/ru/post/1610356/


All Articles