Daniel Lizik
Daniel Lizik

Reputation: 3144

Return rendered ejs view as html string inside service?

I have a service in my sails app that emails the user an activation form. I'm using emailjs to send the email, but I have to stick in a messy html string manually.

I'm relatively new to sails and am having a hard time finding documentation on how to stick some variables into an ejs template and return an html string. I'm not sure if this is a global service in sails (like async) or I need to add a grunt task.

To be more specific this is what I need to do:

views/emailTemplate.ejs

<html>
    <h1>Hello, <%- user %></h1>
</html>

services/generateEmailService.js

module.exports = function() {
    var email = require("emailjs");
    ejs('views/emailTemplate.ejs', {user: "Bob"}, function(html) {
        email.send({to: "[email protected]", from: "[email protected]", html: html});
    });
};

And in your email's source...

<html>
    <h1>Hello, Bob</h1>
</html>

Upvotes: 0

Views: 474

Answers (1)

Meeker
Meeker

Reputation: 5979

I'm not sure exactly what you are trying to solve, but using a sails hook you can initiate the view engine and render one of your views with data.

See also How to render a view into a string in SailsJS?

sails.hooks.view.render()

Upvotes: 1

Related Questions