Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a view into a string in SailsJS?

Tags:

ejs

sails.js

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
    });
  },
like image 870
Igor Zinov'yev Avatar asked Jun 12 '14 14:06

Igor Zinov'yev


Video Answer


1 Answers

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

like image 75
mdunisch Avatar answered Dec 29 '22 04:12

mdunisch