[英]How to render a view to a string?
I want to render a specific view in a sailsjs controller/action which should be sent out as email. 我想在sailsjs控制器/动作中呈现特定视图,该视图应作为电子邮件发送。
I have the following sample action: 我有以下示例操作:
function registerAction(req, res) {
// handle user registration
// email user
sendEmail({
to: newUser.email,
subject: "Welcome",
text: /* VIEW RENDER HERE */
});
// render view to the user
return res.view({
user: newUser
});
}
How can I render a view-template with the tools sailsjs provides so that I don't need to hardcode an email text or use other libraries? 如何使用sailsjs提供的工具呈现视图模板,以便我不需要对电子邮件文本进行硬编码或使用其他库?
Thanks in advance! 提前致谢!
Express' res.render()
is still accessible in your res
object. 您的
res
对象仍然可以访问 res.render()
。
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
});
}
To improve a little on crzrcn's answer, to ensure 为了改善crzrcn的答案,确保一点
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
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.