Can Express with EJS render HTML into a variable (so that I can send it by email)?

I am writing a nodejs application that will send an html email address using emailjs . Basically I provide html to send as a variable that I am attaching to the post.

Instead of creating this variable using a lot of string concatenation, I would just like to visualize the view with express / ejs and save the contents to the variable.

So instead:

messageHtml = '<html>'+ .... message.attach({data: messageHtml, alternative: true}); 

I would like to do something like:

 messageHtml = render('emailTemplate.ejs', viewArgs); message.attach({data: messageHtml, alternative: true}); 

Can this be done, and if so, how ?!

+7
source share
1 answer

Just require ejs directly and use according to example , for example, simplified use (without caching):

 var ejs = require('ejs') , fs = require('fs') , str = fs.readFileSync(__dirname + '/emailTemplate.ejs', 'utf8'); var messageHtml = ejs.render(str, viewArgs); message.attach({data: messageHtml, alternative: true}); 
+11
source

All Articles