如何在SailsJS中将视图渲染为字符串? [英] How to render a view into a string in SailsJS?

查看:108
本文介绍了如何在SailsJS中将视图渲染为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个控制器中,我想呈现具有特定布局的特定视图以发送包含结果字符串的电子邮件,但是显然我不需要向用户显示结果。有没有办法使用我用来渲染视图的EJS引擎来实现此目的?这是我简化的控制器操作:

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


推荐答案

使用视图挂钩

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

});

编辑:右回调

这篇关于如何在SailsJS中将视图渲染为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆