How to render a view to a string?

后端 未结 2 573
谎友^
谎友^ 2020-12-22 03:37

I want to render a specific view in a sailsjs controller/action which should be sent out as email.

I have the following sample action:

function regis         


        
相关标签:
2条回答
  • 2020-12-22 04:05

    To improve a little on crzrcn's answer, to ensure

    function registerAction(req, res) {
       // handle user registration
    
      // don't include the .ejs of the end of your view, this assumes a file in path/to/view.ejs
      res.render('path/to/view', function (err, html) {
        // email user
        sendEmail({
            to: newUser.email,
            subject: "Welcome",
            html: html // rather than text in crzrcn's answer
        });
      }
    
      // render view to the user
      return res.view({
         user: newUser
      });
    }
    
    0 讨论(0)
  • 2020-12-22 04:07

    Express' res.render() is still accessible in your res object.

    function registerAction(req, res) {
       // handle user registration
    
      res.render('/path/to/view', function (err, html) {
        // email user
        sendEmail({
            to: newUser.email,
            subject: "Welcome",
            text: html
        });
      }
    
      // render view to the user
      return res.view({
         user: newUser
      });
    }
    
    0 讨论(0)
提交回复
热议问题