How to render a view into a string in SailsJS?

让人想犯罪 __ 提交于 2019-12-04 03:42:01

问题


In one controller I want to render a certain view with a certain layout to send an email with the resulting string, but I obviously do not need to show the result to the user. Is there a way to use the EJS engine that I'm using to render views to achieve this? Here's my a bit simplified controller action:

  setEmail: function(req, res) {
    var update = {
      activationToken: _getToken(),
      email: req.param('email')
    };

    Profile.update(res.locals.profile.id, update).then(function(profile) {
      res.redirect(profileUrl);
      mailer.sendActivationEmail(
        update.email,
        res.i18n('Подтвердите свой адрес электронной почты'),
        emailString); // <=== Here is where I need the string rendered from a view
    });
  },

回答1:


Use the view-hook

Profile.update(res.locals.profile.id, update).then(function(profile) {
  res.redirect(profileUrl);

  sails.hooks.views.render("viewname",profile, function(err,html){
     if(err) return console.log(err);

     mailer.sendActivationEmail(
       update.email,
       res.i18n('Подтвердите свой адрес электронной почты'),
       html
     );
    })

});

Edit: right callback




回答2:


I think I would rather use a specific email module like this one:

https://github.com/niftylettuce/node-email-templates

Which can access EJS templates



来源:https://stackoverflow.com/questions/24187206/how-to-render-a-view-into-a-string-in-sailsjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!