Igor Zinov'yev
Igor Zinov'yev

Reputation: 3706

How to render a view into a string in SailsJS?

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
    });
  },

Upvotes: 5

Views: 3276

Answers (2)

mdunisch
mdunisch

Reputation: 3697

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

Upvotes: 7

Aur&#233;lien Thieriot
Aur&#233;lien Thieriot

Reputation: 5923

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

Upvotes: 2

Related Questions