Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override compileTemplate #2

Closed
xeroxoid opened this issue Mar 23, 2015 · 6 comments
Closed

Override compileTemplate #2

xeroxoid opened this issue Mar 23, 2015 · 6 comments

Comments

@xeroxoid
Copy link

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!

@xeroxoid xeroxoid changed the title Override compileTenmplate Override compileTemplate Mar 23, 2015
@yads
Copy link
Owner

yads commented Mar 23, 2015

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
Copy link
Author

xeroxoid commented Apr 4, 2015

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
Copy link
Owner

yads commented Apr 7, 2015

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
Copy link
Author

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

@yads
Copy link
Owner

yads commented Apr 16, 2015

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

@yads yads closed this as completed Apr 16, 2015
@xeroxoid
Copy link
Author

True, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants