swig-email-templates

Node.js module for rendering emails with swig templates and email-friendly inline CSS

swig-email-templates

Node.js module for rendering emails with swig templates and email-friendly inline CSS using juice.

Inspired by niftylettuce/node-email-templates.

  • Uses swig, which supports Django-inspired template inheritance.
  • Uses juice, which takes an HTML file and inlines all the <link rel="stylesheet">s and the <style>s.
  • URL rewrite support - you can provide a urlRewriteFn argument to rewrite your links.
  • Text emails - for a template name passed into render(), if a file exists with the same name but a .txt extension it will be rendered separately. If the .txt file does not exist, html-to-text will auto-generate a text version of the html file. This can be disabled with the option text: false.
var path = require('path')
  , emailTemplates = require('swig-email-templates')
 
var options = {
  root: path.join(__dirname, "templates"),
  // any other swig options allowed here 
};
emailTemplates(options, function(errrender) {
  var context = {
    meatballCount: 9001,
  };
  render('meatball-sandwich.html', context, function(errhtmltext) {
    // send html/text email 
  });
});

Installing swig-email-templates through npm will put the swig-email-templates command in your system path, allowing it to be run from any directory.

swig-email-templates render [files] [options]

Where [files] can be any number of input files to process.

The options are:

  • -v, --version: Display the installed version of swig-email-templates

  • -h, --help: Show the help screen

  • -o --output: The directory to output your files to. Defaults to stdout

  • -r, --root: The root location for the files. The default is templates.

  • -j, --json: The file that contains your context, stored in JSON

  • -c, --context: The file that contains your context, stored as a CommonJS module. Used only if -j is not provided.

The following examples renders two files, email1.html and email2.html, which are both contained in the cwd. It uses the context stored in context/main.json for rendering, and places the results in the folder output.

swig-email-templates render email1.html email2.html -r ./ -o output/ -j context/main.json