From ace2e0560c131a05c280ddc8aaea8fc27c6a1e5b Mon Sep 17 00:00:00 2001 From: Nico Swiatecki Date: Mon, 28 May 2018 22:13:54 +0200 Subject: [PATCH] Add setting option for hbs root directory --- README.md | 35 ++++++++++-- lib/__tests__/effe.test.js | 57 ++++++++++++++++++- .../fixtures/effe/withHeaderData/html.hbs | 2 +- .../fixtures/effe/withRootOptions/html.hbs | 6 ++ .../fixtures/effe/withRootOptions/subject.hbs | 1 + .../fixtures/effe/withRootOptions/text.hbs | 1 + .../fixtures/handlebars2/helpers/upcase.js | 14 +++++ .../fixtures/handlebars2/layouts/test.hbs | 5 ++ .../fixtures/handlebars2/partials/test.hbs | 2 + lib/index.js | 23 ++++++-- 10 files changed, 134 insertions(+), 12 deletions(-) create mode 100644 lib/__tests__/fixtures/effe/withRootOptions/html.hbs create mode 100644 lib/__tests__/fixtures/effe/withRootOptions/subject.hbs create mode 100644 lib/__tests__/fixtures/effe/withRootOptions/text.hbs create mode 100644 lib/__tests__/fixtures/handlebars2/helpers/upcase.js create mode 100644 lib/__tests__/fixtures/handlebars2/layouts/test.hbs create mode 100644 lib/__tests__/fixtures/handlebars2/partials/test.hbs diff --git a/README.md b/README.md index 10b804b..1f25e82 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ yarn add email-templates-effe ### Usage -Once installed you need to import the module: +Once installed you need to import the module and set up the root directory for handlebars: ```javascript const Email = require('email-templates'); const path = require('path'); @@ -41,9 +41,7 @@ const email = new Email({ options: { extension: 'hbs', engineSource: engine({ - layouts: path.join(__dirname, 'handlebars/layouts'), - partials: path.join(__dirname, 'handlebars/partials'), - helpers: path.join(__dirname, 'handlebars/helpers') + root: path.join(__dirname, 'handlebars') }) } } @@ -65,6 +63,35 @@ email }); .catch(console.error); ``` +Or you can define all handlebars directory manually with `layouts`, `partials` and `helpers` +```javascript +// ...Other Code + +// Setup +const email = new Email({ + views: { + root: path.join(__dirname, 'root/directory/to/all/email/files'), + options: { + extension: 'hbs', + engineSource: engine({ + layouts: path.join(__dirname, 'handlebars/layouts'), + partials: path.join(__dirname, 'handlebars/partials'), + helpers: path.join(__dirname, 'handlebars/helpers') + }) + } + } +}); + +// ...Other Code +``` + +## Configuration +| configuration option | type | default | description | +|:---------------------|:------:|:-------:|:-----------------------------------------------------------------------------------------------------------------| +| root | string | - | With `root` you can define root directory for handlebars with `layouts`, `partials` and `partials` as suborders. | +| layouts | string | - | With `layouts` you can define an exact handlebars layouts path | +| partials | string | - | With `partials` you can define an exact handlebars partials path | +| partials | string | - | With `partials` you can define an exact handlebars partials path | ## Development diff --git a/lib/__tests__/effe.test.js b/lib/__tests__/effe.test.js index 9ce8eb8..b85aa96 100644 --- a/lib/__tests__/effe.test.js +++ b/lib/__tests__/effe.test.js @@ -100,7 +100,7 @@ describe('Effe', () => { const $ = cheerio.load(o.html, { xmlMode: true }); return $('body table.wrapper p').text(); }) - ).resolves.toBe('Hello Tim!'); + ).resolves.toBe('Hello TIM!'); return expect(_renderAll.then(o => _.trim(o.text))).resolves.toBe('Hello John!'); }); @@ -182,6 +182,61 @@ describe('Effe', () => { return expect(_renderAll.then(o => _.trim(o.text))).resolves.toBe('Hello John!'); }); + it('should set up hbs root directory', function() { + const email = new Email({ + views: { + root: path.join(__dirname, 'fixtures/effe'), + options: { + extension: 'hbs', + engineSource: engine({ + root: path.join(__dirname, 'fixtures/handlebars2') + }) + } + } + }); + + expect( + email + .renderAll('withRootOptions', { + name: 'John', + layout: 'test' + }) + .then(o => _.isObject(o)) + ).resolves.toBe(true); + expect( + email + .renderAll('withRootOptions', { name: 'John', layout: 'test' }) + .then(o => Object.keys(o)) + ).resolves.toEqual(expect.arrayContaining(['subject', 'html', 'text'])); + expect( + email.renderAll('withRootOptions', { name: 'John', layout: 'test' }).then(o => { + const $ = cheerio.load(o.html, { xmlMode: true }); + return $('html').length; + }) + ).resolves.toEqual(0); + + expect( + email.renderAll('withRootOptions', { name: 'John', layout: 'test' }).then(o => { + const $ = cheerio.load(o.html, { xmlMode: true }); + return $('div h1').html(); + }) + ).resolves.toEqual('This is a other layout'); + + expect( + email.renderAll('withRootOptions', { name: 'John', layout: 'test' }).then(o => { + const $ = cheerio.load(o.html, { xmlMode: true }); + return $('.wrapper h2').html(); + }) + ).resolves.toEqual('Hello TIM'); + + expect( + email.renderAll('withRootOptions', { name: 'John', layout: 'test' }).then(o => { + const $ = cheerio.load(o.html, { xmlMode: true }); + return $('.wrapper p').html(); + }) + ).resolves.toEqual('This is a hbs partial'); + }); + it('should have no engine options', function() { const email = new Email({ views: { diff --git a/lib/__tests__/fixtures/effe/withHeaderData/html.hbs b/lib/__tests__/fixtures/effe/withHeaderData/html.hbs index c0c3676..d92332f 100644 --- a/lib/__tests__/fixtures/effe/withHeaderData/html.hbs +++ b/lib/__tests__/fixtures/effe/withHeaderData/html.hbs @@ -3,5 +3,5 @@ name: Tim --- -

Hello {{name}}!

+

Hello {{upcase name}}!

diff --git a/lib/__tests__/fixtures/effe/withRootOptions/html.hbs b/lib/__tests__/fixtures/effe/withRootOptions/html.hbs new file mode 100644 index 0000000..8fadde5 --- /dev/null +++ b/lib/__tests__/fixtures/effe/withRootOptions/html.hbs @@ -0,0 +1,6 @@ +--- +name: Tim +--- + + {{> test}} + diff --git a/lib/__tests__/fixtures/effe/withRootOptions/subject.hbs b/lib/__tests__/fixtures/effe/withRootOptions/subject.hbs new file mode 100644 index 0000000..6cb905f --- /dev/null +++ b/lib/__tests__/fixtures/effe/withRootOptions/subject.hbs @@ -0,0 +1 @@ +Hello {{name}}! diff --git a/lib/__tests__/fixtures/effe/withRootOptions/text.hbs b/lib/__tests__/fixtures/effe/withRootOptions/text.hbs new file mode 100644 index 0000000..6cb905f --- /dev/null +++ b/lib/__tests__/fixtures/effe/withRootOptions/text.hbs @@ -0,0 +1 @@ +Hello {{name}}! diff --git a/lib/__tests__/fixtures/handlebars2/helpers/upcase.js b/lib/__tests__/fixtures/handlebars2/helpers/upcase.js new file mode 100644 index 0000000..9a3281d --- /dev/null +++ b/lib/__tests__/fixtures/handlebars2/helpers/upcase.js @@ -0,0 +1,14 @@ +const _ = require('lodash'); +const helpers = module.exports; + +helpers.uppercase = function(str) { + if (_.isObject(str) && str.fn) { + return str.fn(this).toUpperCase(); + } + if (!_.isString(str)) return ''; + return str.toUpperCase(); +}; + +helpers.upcase = function() { + return helpers.uppercase.apply(this, arguments); +}; diff --git a/lib/__tests__/fixtures/handlebars2/layouts/test.hbs b/lib/__tests__/fixtures/handlebars2/layouts/test.hbs new file mode 100644 index 0000000..f63571a --- /dev/null +++ b/lib/__tests__/fixtures/handlebars2/layouts/test.hbs @@ -0,0 +1,5 @@ +
+

This is a other layout

+ + {{> body}} +
diff --git a/lib/__tests__/fixtures/handlebars2/partials/test.hbs b/lib/__tests__/fixtures/handlebars2/partials/test.hbs new file mode 100644 index 0000000..31672d4 --- /dev/null +++ b/lib/__tests__/fixtures/handlebars2/partials/test.hbs @@ -0,0 +1,2 @@ +

Hello {{upcase name}}

+

This is a hbs partial

diff --git a/lib/index.js b/lib/index.js index 769c2c5..a77e1bc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -14,13 +14,24 @@ function Effe(options = {}) { this.Handlebars = require('handlebars'); this.layouts = {}; - if (this.options.layouts) { - this._loadLayouts([path.join(__dirname, 'hbs/layouts'), this.options.layouts]); + const layoutsPath = [path.join(__dirname, 'hbs/layouts')]; + const partialsPath = []; + const helpersPath = []; + + if (this.options.root) { + const basePath = this.options.root; + layoutsPath.push(path.join(basePath, 'layouts')); + partialsPath.push(path.join(basePath, 'partials')); + helpersPath.push(path.join(basePath, 'helpers')); } else { - this._loadLayouts(path.join(__dirname, 'hbs/layouts')); + if (this.options.layouts) layoutsPath.push(this.options.layouts); + if (this.options.partials) partialsPath.push(this.options.partials); + if (this.options.helpers) helpersPath.push(this.options.helpers); } - if (this.options.partials) this._loadPartials(this.options.partials); - if (this.options.helpers) this._loadHelpers(this.options.helpers); + + this._loadLayouts(layoutsPath); + if (!_.isEmpty(partialsPath)) this._loadPartials(partialsPath); + if (!_.isEmpty(helpersPath)) this._loadHelpers(helpersPath); } Effe.prototype._loadLayouts = require('./loadLayouts'); @@ -30,7 +41,7 @@ Effe.prototype._render = require('./render').render; /** * Make the object with the engines - * @param {Object} effe + * @param {object} effe * @return {Function} */ function makeRenderer(effe) {