Skip to content

Commit

Permalink
Add setting option for hbs root directory
Browse files Browse the repository at this point in the history
  • Loading branch information
snics committed May 28, 2018
1 parent c4b3eb2 commit ace2e05
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 12 deletions.
35 changes: 31 additions & 4 deletions README.md
Expand Up @@ -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');
Expand All @@ -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')
})
}
}
Expand All @@ -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

Expand Down
57 changes: 56 additions & 1 deletion lib/__tests__/effe.test.js
Expand Up @@ -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!');
});

Expand Down Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/fixtures/effe/withHeaderData/html.hbs
Expand Up @@ -3,5 +3,5 @@ name: Tim
---

<wrapper>
<p>Hello {{name}}!</p>
<p>Hello {{upcase name}}!</p>
</wrapper>
6 changes: 6 additions & 0 deletions lib/__tests__/fixtures/effe/withRootOptions/html.hbs
@@ -0,0 +1,6 @@
---
name: Tim
---
<wrapper>
{{> test}}
</wrapper>
1 change: 1 addition & 0 deletions lib/__tests__/fixtures/effe/withRootOptions/subject.hbs
@@ -0,0 +1 @@
Hello {{name}}!
1 change: 1 addition & 0 deletions lib/__tests__/fixtures/effe/withRootOptions/text.hbs
@@ -0,0 +1 @@
Hello {{name}}!
14 changes: 14 additions & 0 deletions 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);
};
5 changes: 5 additions & 0 deletions lib/__tests__/fixtures/handlebars2/layouts/test.hbs
@@ -0,0 +1,5 @@
<div>
<h1>This is a other layout</h1>

{{> body}}
</div>
2 changes: 2 additions & 0 deletions lib/__tests__/fixtures/handlebars2/partials/test.hbs
@@ -0,0 +1,2 @@
<h2>Hello {{upcase name}}</h2>
<p>This is a hbs partial</p>
23 changes: 17 additions & 6 deletions lib/index.js
Expand Up @@ -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');
Expand All @@ -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) {
Expand Down

0 comments on commit ace2e05

Please sign in to comment.