user1043646
user1043646

Reputation:

Express4 handlebars compile template to variable

I'm currently rendering a handlebars templates to display pages on my node server. I've set the view engine to handlebars

res.render('product', vars);

However, I would like to run the compiled HTML template through juice2 so that the styles are inlined. Because the page is rendered on request, I cannot format the HTML before being sent to the client.

Is there any way of rendering the template server side (to a variable preferably, formatting the content in juice2 and then outputting them)

Upvotes: 0

Views: 552

Answers (1)

kawadhiya21
kawadhiya21

Reputation: 2528

You can do something like this:

res.render('product', vars, function(err, html) {
    juice.juiceContent(html, options, function(err, finalHTML) {
        res.send(finalHTML); 
    })
});
// options.url = base domain like http://example.com

References:

  1. https://www.npmjs.com/package/juice2#juicejuicecontenthtml-options-callback
  2. http://expressjs.com/en/api.html#res

Upvotes: 1

Related Questions