yads / nodemailer-express-handlebars

A plugin for nodemailer that uses express-handlebars view engine to generate emails
87 stars 30 forks source link

Override compileTemplate #2

Closed xeroxoid closed 9 years ago

xeroxoid commented 9 years ago

Hi,

I am trying to override/use the compileTemplate of express-handlebars in order to inline all the CSS styles with juice (found the way here).

I tried this:

...
var hbs = require('nodemailer-express-handlebars');
var juice = require('juice');

var compileTemplate = hbs.compileTemplate.bind(hbs);

hbs.compileTemplate = function (template, options, callback) {
    // Pre-process template here.
    compileTemplate(template, options, function (err, compiled) {
        // Post-process template here.
        callback(err, juice(compiled));
    });
};

...

but obviously I would need to require 'express-handlebars' and not 'nodemailer-express-handlebars'. But is there a way to do that without installing the extra package (as it is already included with this one)?

Thanks!

yads commented 9 years ago

No, but there is nothing wrong with requiring express-handlebars and passing that instance into this plugin. What is the aversion with doing that?

xeroxoid commented 9 years ago

Could you please provide a snippet on how to do that? I am trying this

...
var nodemailer = require('nodemailer');
var sgTransport = require('nodemailer-sendgrid-transport');
var expHbs = require('express-handlebars');
var nodemailerExpHbs = require('nodemailer-express-handlebars');
var juice = require('juice');

var sgOptions = {
  auth: {
    api_user: process.env.SG_AUTH_API_USER,
    api_key: process.env.SG_AUTH_API_KEY,
  }
};

//Testing out instance vs factory
var exFactory = expHbs({ /* config */ });
var exInstance = expHbs.create({ /* config */ });

var compileTemplate = expHbs.compileTemplate.bind(expHbs); // <-- fails here

expHbs.compileTemplate = function (template, options, callback) {
  // Pre-process template here.
  compileTemplate(template, options, function (err, compiled) {
    // Post-process template here.
    callback(err, juice(compiled));
  });
};

var viewEngineOptions = expHbs.create({
  extname: '.hbs',
  layoutsDir: 'server/views/email/',
  defaultLayout: 'template',
  partialsDir: 'server/views/email/partials/'
});

var nodemailerExpHbsOptions = {
  viewEngine: viewEngineOptions,
  viewPath: 'server/views/email/',
  extName: '.hbs'
};

var mailer = nodemailer.createTransport(sgTransport(sgOptions));
mailer.use('compile', nodemailerExpHbs(nodemailerExpHbsOptions));
...

but it fails miserably as .compileTemplate does not seem to be available... Sorry for posting a question here.

yads commented 9 years ago

Instead of supplying the options into the viewEngine add the actual express-handlebars instance like so

var nodemailerExpHbsOptions = {
  viewEngine: exInstance,
  viewPath: 'server/views/email/',
  extName: '.hbs'
};

I'm not sure what's failing on this line

var compileTemplate = expHbs.compileTemplate.bind(expHbs); // <-- fails here

It's beyond the scope of this plugin. I'd check with express-handlebars maintainer.

xeroxoid commented 9 years ago

What fails is that compileTemplate is not defined as a method of expHbs (i.e. var expHbs = require('express-handlebars');)

yads commented 9 years ago

This is really something you probably should bring up with the maintainer of that repository. It's beyond the scope of this plugin. Closing.

xeroxoid commented 9 years ago

True, thanks.