From c0a1beb89780fc0269df24e91ce1671ec9237304 Mon Sep 17 00:00:00 2001 From: SHAILENDRA SHARMA Date: Sat, 26 Sep 2015 22:07:53 +0530 Subject: [PATCH 1/8] Support subject template too. --- src/email-template.js | 27 ++++++++++++++++++++++----- src/main.js | 4 ++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/email-template.js b/src/email-template.js index c23f69e..9763b83 100644 --- a/src/email-template.js +++ b/src/email-template.js @@ -29,11 +29,11 @@ export default class EmailTemplate { } _loadTemplates () { - return P.map(['html', 'text', 'style'], (type) => { + return P.map(['html', 'text', 'style', 'subject'], (type) => { return readContents(this.path, type) }) .then((files) => { - let [html, text, style] = files + let [html, text, style, subject] = files if (!html && !text) { let err = new Error(`Neither html nor text template files found or are both empty in path ${this.dirname}`) @@ -56,6 +56,11 @@ export default class EmailTemplate { } this.files.style = style + if (subject) { + debug('Found subject %s in %s', basename(subject.filename), this.dirname) + } + this.files.subject = subject + debug('Finished loading template') }) } @@ -71,6 +76,17 @@ export default class EmailTemplate { .nodeify(callback) } + renderSubject (locals, callback) { + debug('Rendering subject') + return this._init() + .then(() => { + if (!this.files.subject) return null + return renderFile(this.files.subject, locals) + }) + .tap(() => debug('Finished rendering subject')) + .nodeify(callback) + } + renderHtml (locals, callback) { debug('Rendering HTML') return this._init() @@ -101,12 +117,13 @@ export default class EmailTemplate { return P.all([ this.renderHtml(locals), - this.renderText(locals) + this.renderText(locals), + this.renderSubject(locals) ]) .then((rendered) => { - let [html, text] = rendered + let [html, text, subject] = rendered return { - html, text + html, text, subject } }) .nodeify(callback) diff --git a/src/main.js b/src/main.js index fd76616..961449a 100644 --- a/src/main.js +++ b/src/main.js @@ -38,14 +38,14 @@ function template (templateDirectory, options) { return callback(null, function (locals, dir, next) { et.render(locals, function (err, result) { result = result || {} - next(err, result.html, result.text) + next(err, result.html, result.text, result.subject) }) }) } et.render(locals, function (err, result) { result = result || {} - callback(err, result.html, result.text) + callback(err, result.html, result.text, result.subject) }) } } From fb3c55b722622fe89ec9ccce9c65b1acc91750d3 Mon Sep 17 00:00:00 2001 From: Shailendra Sharma Date: Sun, 27 Sep 2015 20:22:13 +0530 Subject: [PATCH 2/8] Updated to include email subject --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3dba5af..5657ece 100644 --- a/README.md +++ b/README.md @@ -98,13 +98,14 @@ npm install -S [ejs|jade|nunjucks|handlebars|emblem|dust-linkedin] ``` 4. Add the following files inside the template's folder: - * `html.{{ext}}` (**required**) - * `text.{{ext}}` (**optional**) - * `style.{{ext}}`(**optional**) + * `html.{{ext}}` (**required**) - for html format of email + * `text.{{ext}}` (**optional**) - for text format of email + * `style.{{ext}}`(**optional**) - styles for html format + * `subject.{{ext}}`(**optional**) - for subject of email > **See [supported template engines](#supported-template-engines) for possible template engine extensions (e.g. `.ejs`, `.jade`, `.nunjucks`) to use for the value of `{{ext}}` above.** - > You may prefix any file name with anything you like to help you identify the files more easily in your IDE. The only requirement is that the filename contains `html.`, `text.`, and `style.` respectively. + > You may prefix any file name with anything you like to help you identify the files more easily in your IDE. The only requirement is that the filename contains `html.`, `text.`, `style.`, and `subject.` respectively. 5. You may use the `include` directive from [ejs][ejs] (for example, to include a common header or footer). See the `/examples` folder for details. @@ -172,6 +173,7 @@ async.each(users, function (user, next) { if (err) return next(err) // result.html // result.text + // result.subject }) }, function (err) { // @@ -211,8 +213,10 @@ Promise.all(templates) .then(function (results) { console.log(results[0].html) console.log(results[0].text) + console.log(results[0].subject) console.log(results[1].html) console.log(results[1].text) + console.log(results[1].subject) }) ``` From 536e456efe6698c91e61f8fb2fbe1689c795c31b Mon Sep 17 00:00:00 2001 From: Shailendra Sharma Date: Mon, 28 Sep 2015 08:28:41 +0530 Subject: [PATCH 3/8] Fix failing test case for inline CSS(less) Fix failing test case for inline CSS(less) --- test/testMain.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testMain.js b/test/testMain.js index c0ebbdc..f058711 100644 --- a/test/testMain.js +++ b/test/testMain.js @@ -129,7 +129,7 @@ describe('Email templates', function () { it('html(jade) with inline CSS(less)', function (done) { var html = 'h4= item' - var css = '@color: #ccc; h4 { color: @color }' + var css = '@color: #cccccc; h4 { color: @color }' fs.writeFileSync(path.join(templateDir, templateName, 'html.jade'), html) fs.writeFileSync(path.join(templateDir, templateName, 'style.less'), css) From ea61d6c76b9e5777ebdd699281bb04ffcebfc7e6 Mon Sep 17 00:00:00 2001 From: SHAILENDRA SHARMA Date: Mon, 28 Sep 2015 08:41:40 +0530 Subject: [PATCH 4/8] Fix existing test case for color code mismatch --- test/testTemplateManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testTemplateManager.js b/test/testTemplateManager.js index f77d4c2..1035bf9 100644 --- a/test/testTemplateManager.js +++ b/test/testTemplateManager.js @@ -152,7 +152,7 @@ describe('Template manager', function () { // Write out some test LESS files. fs.writeFileSync(testMainLessFile, '@import "includes.less";') - fs.writeFileSync(testIncludesFile, '.body { color: #333}') + fs.writeFileSync(testIncludesFile, '.body { color: #333333}') var file = { filename: testMainLessFile, From 38b7259c686d06feed487d7be3ae7d57fc258fad Mon Sep 17 00:00:00 2001 From: SHAILENDRA SHARMA Date: Wed, 30 Sep 2015 14:22:09 +0530 Subject: [PATCH 5/8] Updated with test cases for subject --- test/testMain.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/test/testMain.js b/test/testMain.js index f058711..8ecb6e8 100644 --- a/test/testMain.js +++ b/test/testMain.js @@ -67,6 +67,26 @@ describe('Email templates', function () { }) }) + it('html, text, and subject files', function (done) { + var html = '

<%= item%>

' + var text = '<%= item%>' + var subject = 'Hello <%= item%>' + fs.writeFileSync(path.join(templateDir, templateName, 'html.ejs'), html) + fs.writeFileSync(path.join(templateDir, templateName, 'text.ejs'), text) + fs.writeFileSync(path.join(templateDir, templateName, 'subject.ejs'), subject) + + emailTemplates(templateDir, function (err, template) { + expect(err).to.be.null + template(templateName, {item: 'test'}, function (err, html, text, subject) { + expect(err).to.be.null + expect(html).to.equal('

test

') + expect(text).to.equal('test') + expect(subject).to.equal('Hello test') + done() + }) + }) + }) + it('html and text files with custom names', function (done) { var html = '

<%= item%>

' var text = '<%= item%>' @@ -84,6 +104,26 @@ describe('Email templates', function () { }) }) + it('html, text, and subject files with custom names', function (done) { + var html = '

<%= item%>

' + var text = '<%= item%>' + var subject = 'Hello <%= item%>' + fs.writeFileSync(path.join(templateDir, templateName, 'custom-filename-html.ejs'), html) + fs.writeFileSync(path.join(templateDir, templateName, 'custom-filename-text.ejs'), text) + fs.writeFileSync(path.join(templateDir, templateName, 'custom-filename-subject.ejs'), subject) + + emailTemplates(templateDir, function (err, template) { + expect(err).to.be.null + template(templateName, {item: 'test'}, function (err, html, text, subject) { + expect(err).to.be.null + expect(html).to.equal('

test

') + expect(text).to.equal('test') + expect(subject).to.equal('Hello test') + done() + }) + }) + }) + it('html with style element and juiceOptions', function (done) { var html = '

<%= item%>

' var css = 'h4 { color: blue; }' @@ -255,6 +295,20 @@ describe('Email templates', function () { }) }) + it('on missing html, text, and subject file', function (done) { + emailTemplates(templateDir, function (err, template) { + expect(err).to.be.null + template(templateName, {item: 'test'}, function (err, html, text, subject) { + expect(err.code).to.equal('ENOENT') + expect(html).to.be.undefined + expect(text).to.be.undefined + expect(subject).to.be.undefined + + done() + }) + }) + }) + it('on empty html and text file', function (done) { fs.writeFileSync(path.join(templateDir, templateName, 'html.ejs'), '') fs.writeFileSync(path.join(templateDir, templateName, 'text.ejs'), '') @@ -269,5 +323,22 @@ describe('Email templates', function () { }) }) }) + + it('on empty html, text, and subject file', function (done) { + fs.writeFileSync(path.join(templateDir, templateName, 'html.ejs'), '') + fs.writeFileSync(path.join(templateDir, templateName, 'text.ejs'), '') + fs.writeFileSync(path.join(templateDir, templateName, 'subject.ejs'), '') + + emailTemplates(templateDir, function (err, template) { + expect(err).to.be.null + template(templateName, {item: 'test'}, function (err, html, text, subject) { + expect(html).to.be.undefined + expect(text).to.be.undefined + expect(subject).to.be.undefined + expect(err.message).to.contain('both empty in path') + done() + }) + }) + }) }) }) From 34130708985952e9fcb70e300717f059dc8079cc Mon Sep 17 00:00:00 2001 From: SHAILENDRA SHARMA Date: Mon, 12 Oct 2015 10:23:50 +0530 Subject: [PATCH 6/8] Created a new package email-templates-v2 for the extended functionality. I first created a pull request on the original repository, but pull requests is not even getting reviewed there. --- package.json | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index f0806fc..4e34aeb 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,13 @@ { - "name": "email-templates", + "name": "email-templates-v2", "description": "Node.js module for rendering beautiful emails with ejs, jade, swig, hbs, or handlebars templates and email-friendly inline CSS using juice.", - "version": "2.0.1", - "author": "Nick Baugh ", + "version": "2.0.2", + "author": "Shailendra Sharma ", "contributors": [ + { + "name": "Shailendra Sharma", + "email": "shailendra.sharma@gmail.com" + }, { "name": "Nick Baugh", "email": "niftylettuce@gmail.com" @@ -38,12 +42,16 @@ "email-templates", "juice", "inline", - "css" + "css", + "subject template", + "html template", + "text template", + "v2" ], - "homepage": "https://github.com/niftylettuce/node-email-templates", + "homepage": "https://github.com/snow01/node-email-templates-v2", "repository": { "type": "git", - "url": "https://github.com/niftylettuce/node-email-templates.git" + "url": "https://github.com/snow01/node-email-templates-v2.git" }, "engines": { "node": ">= 0.10" @@ -97,6 +105,6 @@ }, "license": "MIT", "bugs": { - "url": "https://github.com/niftylettuce/node-email-templates/issues/new" + "url": "https://github.com/snow01/node-email-templates-v2/issues/new" } } From db68a9939b84a6efbef840714d064d945fcc90b6 Mon Sep 17 00:00:00 2001 From: Shailendra Sharma Date: Mon, 12 Oct 2015 10:32:21 +0530 Subject: [PATCH 7/8] Updated README for renamed package --- README.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5657ece..fc78269 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,11 @@ Node.js NPM package for rendering beautiful emails with your template engine and CSS pre-processor of choice coupled with email-friendly inline CSS using [juice][juice]. -> Enjoy this package? Check out [eskimo][eskimo] and [express-cdn][express-cdn], and follow [@niftylettuce](http://twitter.com/niftylettuce)! +> Enjoy this package? Follow [@yeliahs](https://twitter.com/yeliahs)! +## Note + +This package is extended version of [email-templates](https://github.com/niftylettuce/node-email-templates). v2 adds support for templating subjects. ## Index @@ -34,7 +37,7 @@ For customizable, pre-built email templates, see [Email Blueprints][email-bluepr #### Supported Template Engines -node-email-templates uses [consolidate.js][consolidate], and therefore supports a vast array of template modules. Please see [consolidate.js][consolidate] for the impressive full list. +email-templates-v2 uses [consolidate.js][consolidate], and therefore supports a vast array of template modules. Please see [consolidate.js][consolidate] for the impressive full list. #### Supported CSS Pre-processors @@ -56,10 +59,10 @@ Developing on OS X or Ubuntu/Linux is recommended, but if you only have access t ## Installation -Install `email-templates` and the engines you wish to use by adding them to your `package.json` dependencies. +Install `email-templates-v2` and the engines you wish to use by adding them to your `package.json` dependencies. ```bash -npm install --save email-templates +npm install --save email-templates-v2 # See https://www.npmjs.com/package/consolidate for a full list of available template engines npm install -S [ejs|jade|nunjucks|handlebars|emblem|dust-linkedin] ``` @@ -70,7 +73,7 @@ npm install -S [ejs|jade|nunjucks|handlebars|emblem|dust-linkedin] 1. Install the module for your respective project: ```bash - npm install --save email-templates@2 + npm install --save email-templates-v2@2 ``` 2. Install the template engine you intend to use: @@ -222,7 +225,7 @@ Promise.all(templates) ### More -Please check the [examples directory](https://github.com/niftylettuce/node-email-templates/tree/master/examples) +Please check the [examples directory](https://github.com/snow01/node-email-templates-v2/tree/master/examples) ## Contributors @@ -232,6 +235,7 @@ Please check the [examples directory](https://github.com/niftylettuce/node-email * Jason Sims * Miguel Mota * Jeduan Cornejo +* Shailendra Sharma > Full list of contributors can be found on the [GitHub Contributor Graph][gh-graph] From b7161e419712d1c313a65f13ea9e0fdf90f2be39 Mon Sep 17 00:00:00 2001 From: Shailendra Sharma Date: Mon, 12 Oct 2015 10:37:58 +0530 Subject: [PATCH 8/8] Update README --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index fc78269..4111490 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Node.js NPM package for rendering beautiful emails with your template engine and ## Note -This package is extended version of [email-templates](https://github.com/niftylettuce/node-email-templates). v2 adds support for templating subjects. +This package (v2) is extended version of [email-templates](https://github.com/niftylettuce/node-email-templates). v2 adds support for templating subjects. ## Index @@ -260,17 +260,17 @@ Please check the [examples directory](https://github.com/snow01/node-email-templ [express-cdn]: https://github.com/niftylettuce/express-cdn [license-image]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat [license-url]: LICENSE -[gh-graph]: https://github.com/niftylettuce/node-email-templates/graphs/contributors -[npm-image]: http://img.shields.io/npm/v/email-templates.svg?style=flat -[npm-url]: https://npmjs.org/package/email-templates -[npm-downloads]: http://img.shields.io/npm/dm/email-templates.svg?style=flat -[travis-url]: http://travis-ci.org/niftylettuce/node-email-templates -[travis-image]: http://img.shields.io/travis/niftylettuce/node-email-templates.svg?style=flat -[codeclimate-image]: http://img.shields.io/codeclimate/github/niftylettuce/node-email-templates.svg?style=flat -[codeclimate-url]: https://codeclimate.com/github/niftylettuce/node-email-templates?branch=master -[coveralls-image]: https://img.shields.io/coveralls/niftylettuce/node-email-templates.svg?style=flat -[coveralls-url]: https://coveralls.io/r/niftylettuce/node-email-templates?branch=master -[gitter-url]: https://gitter.im/niftylettuce/node-email-templates +[gh-graph]: https://github.com/snow01/node-email-templates-v2/graphs/contributors +[npm-image]: http://img.shields.io/npm/v/email-templates-v2.svg?style=flat +[npm-url]: https://npmjs.org/package/email-templates-v2 +[npm-downloads]: http://img.shields.io/npm/dm/email-templates-v2.svg?style=flat +[travis-url]: http://travis-ci.org/snow01/node-email-templates-v2 +[travis-image]: http://img.shields.io/travis/snow01/node-email-templates-v2.svg?style=flat +[codeclimate-image]: http://img.shields.io/codeclimate/github/snow01/node-email-templates-v2.svg?style=flat +[codeclimate-url]: https://codeclimate.com/github/snow01/node-email-templates-v2?branch=master +[coveralls-image]: https://img.shields.io/coveralls/snow01/node-email-templates-v2.svg?style=flat +[coveralls-url]: https://coveralls.io/r/snow01/node-email-templates-v2?branch=master +[gitter-url]: https://gitter.im/snow01/node-email-templates-v2 [gitter-image]: http://img.shields.io/badge/chat-online-brightgreen.svg?style=flat [eskimo]: http://eskimo.io [nifty-conventions]: https://github.com/niftylettuce/nifty-conventions