Luis Delgado
Luis Delgado

Reputation: 3734

Getting HTML code from Handlebars template on server-side

My NodeJS is supposed to send html email notifications from users. In order to keep the code clean, I would like to get the HTML code for those emails from handlebars templates, instead of hardcoding the HTML code into the sendEMail methods (which just looks plain ugly).

I need the server to get the HTML from those .handlebars files, but this is not working. Here is how I am trying to get the HTML code

var handlebars = require('express-handlebars').create({});
var context = {
        title: 'T'
    }
var template = handlebars.render('../views/emails/passwordReset.handlebars', context);

console.log(template);

Console.log(template) is returning {}

Here is the .handlebars code:

<h1>{{title}}</h1>

Can anyone provide guidance on what is wrong with this code?

Upvotes: 2

Views: 848

Answers (1)

Ethan Brown
Ethan Brown

Reputation: 27282

handlebars.render doesn't return the rendered text; rather, it returns a promise. You can subscribe to the promise's success event to get the output:

handlebars.render('test.handlebars', context)
    .then(function(data) {
        console.log(data);
    })
    .catch(function(err) {
        console.error(err);
    });

Upvotes: 1

Related Questions