handlebars.jsemailpartials

Unable to use partials in handlebars Email templates


I am trying to use partials inside email templates. I am using 'express-handlebars' as templating engine at the app level

exphbs = require('express-handlebars')
var viewsPath = path.join(__dirname, '/app/views');  
app.set('views', viewsPath);  
var hbs = exphbs.create({  
  defaultLayout: 'main',  
  layoutsDir: viewsPath + '/layouts',  
  partialsDir: viewsPath + '/partials'
});  
app.engine('handlebars', hbs.engine);  
app.set('view engine', 'handlebars');

And I have a helper for sending emails as below:

var templateDir = path.join(__dirname, '../views/email_templates');
var EmailTemplate = require('email-templates').EmailTemplate;

function sendEmail(options,done){
      var template = new EmailTemplate(templateDir + "/" + options.template);
      template.render(options, function(err, result){
        var message = {
          'to' : [options.toEmail],
          'from_email' : 'support@xyz.com',
          'from_name' : 'XYZ',
          'headers' : {
            "Reply-To" : 'support@xyz.com'
          },
          'subject' : options.subject,
          'html' : result.html
        };

          var to_address = message.to[0].email;
          ses_client.sendEmail({
            'from' : message.from_email,
            'replyTo' : message.from_email,
            'subject' : message.subject,  
            'to' : to_address,
            'message' : message.html
            },function (err, data,res){
              if(err) logger.log('error', 'Error in sending email' + err);
              return done();
          });
      });
}

When I include a partial in my html.handlebars template as {{> myPartial}}, I get the following error: The partial myPartial could not be found

I have verified that the partial file is at the same path as specified above at "partialsDir: viewsPath + '/partials'"

How can I get my partials to be rendered inside the email templates?

I have tried using https://stackoverflow.com/a/12700409/6165688 but still I get the same error of partial not being found.


Solution

  • Finally got the partials working for my E-mail templates. I referred to this link and switched to nodemailer npm from the email-templates that I was using earlier.