Skip to content

Commit

Permalink
Merge pull request #155 from allouis/add-support-for-dynamic-template…
Browse files Browse the repository at this point in the history
…-options-v1x

Adds support for local template options
  • Loading branch information
allouis authored Apr 3, 2019
2 parents 10f2b03 + 5460a5a commit ebb6112
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 2 deletions.
18 changes: 17 additions & 1 deletion lib/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var path = require('path');
var readdirp = require('readdirp');
var handlebars = require('handlebars');
var async = require('./async');
var _ = require('lodash');

/**
* Regex pattern for layout directive. {{!< layout }}
Expand Down Expand Up @@ -397,10 +398,22 @@ ExpressHbs.prototype.registerAsyncHelper = function(name, fn) {
});
};

ExpressHbs.prototype.getTemplateOptions = function() {
return this._options.templateOptions;
};

ExpressHbs.prototype.updateTemplateOptions = function(templateOptions) {
this._options.templateOptions = templateOptions;
};

ExpressHbs.prototype.getLocalTemplateOptions = function(locals) {
return locals._templateOptions || {};
};

ExpressHbs.prototype.updateLocalTemplateOptions = function(locals, localTemplateOptions) {
return locals._templateOptions = localTemplateOptions;
};

/**
* Creates a new instance of ExpressHbs.
*/
Expand Down Expand Up @@ -480,7 +493,10 @@ ExpressHbs.prototype.___express = function ___express(filename, source, options,
var res;

try {
res = template(locals, self._options.templateOptions);
var localTemplateOptions = self.getLocalTemplateOptions(locals);
var localsClone = _.extend({}, locals);
self.updateLocalTemplateOptions(localsClone, undefined);
res = template(localsClone, _.merge({}, self._options.templateOptions, localTemplateOptions));
} catch (err) {
if (err.message) {
err.message = '[' + template.__filename + '] ' + err.message;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"dependencies": {
"handlebars": "4.0.13",
"js-beautify": "1.6.8",
"lodash": "4.17.11",
"readdirp": "2.1.0"
}
}
2 changes: 1 addition & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function stripWs(s) {
function createLocals(which, viewsDir, locals) {
if (!locals) locals = {};
var opts = {};
if (which === 'express3') {
if (which === 'express3' || which === 'express4') {
opts.settings = {
views: viewsDir
};
Expand Down
119 changes: 119 additions & 0 deletions test/localTemplateOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'use strict';
var assert = require('assert');
var path = require('path');
var hbs = require('..');
var H = require('./helpers');

describe('local template options', function () {
var dirname = path.join(__dirname, 'localTemplateOptions');

describe('express3', function () {

it('merges res.locals._templateOptions with the self._templateOptions', function (done) {
var instance = hbs.create();
var render = instance.express3({});
instance.updateTemplateOptions({
data: {
greeting: 'Hello,',
firstName: 'Freddy',
lastName: 'Krueger'
}
});

var locals = H.createLocals('express3', dirname, {
_templateOptions: {
data: {
lastName: 'Mercury'
}
}
});

render(path.join(dirname, 'template.hbs'), locals, function (err, html) {
assert.ifError(err);
assert.strictEqual(H.stripWs(html), H.stripWs('Hello, Freddy Mercury'));
done(err);
});
});

it('removes _templateOptions from the locals data', function (done) {
var instance = hbs.create();
var render = instance.express3({});
instance.updateTemplateOptions({
data: {
greeting: 'Hello,',
firstName: 'Freddy',
lastName: 'Krueger'
}
});

var locals = H.createLocals('express3', dirname, {
_templateOptions: {
data: {
lastName: 'Mercury'
}
}
});

render(path.join(dirname, 'data-access-template.hbs'), locals, function (err, html) {
assert.ifError(err);
assert.strictEqual(H.stripWs(html), H.stripWs(''));
done(err);
});
});
});

describe('express4', function () {

it('merges res.locals._templateOptions with the self._templateOptions', function (done) {
var instance = hbs.create();
var render = instance.express4({});
instance.updateTemplateOptions({
data: {
greeting: 'Hello,',
firstName: 'Freddy',
lastName: 'Krueger'
}
});

var locals = H.createLocals('express4', dirname, {
_templateOptions: {
data: {
lastName: 'Mercury'
}
}
});

render(path.join(dirname, 'template.hbs'), locals, function (err, html) {
assert.ifError(err);
assert.strictEqual(H.stripWs(html), H.stripWs('Hello, Freddy Mercury'));
done(err);
});
});

it('removes _templateOptions from the locals data', function (done) {
var instance = hbs.create();
var render = instance.express3({});
instance.updateTemplateOptions({
data: {
greeting: 'Hello,',
firstName: 'Freddy',
lastName: 'Krueger'
}
});

var locals = H.createLocals('express3', dirname, {
_templateOptions: {
data: {
lastName: 'Mercury'
}
}
});

render(path.join(dirname, 'data-access-template.hbs'), locals, function (err, html) {
assert.ifError(err);
assert.strictEqual(H.stripWs(html), H.stripWs(''));
done(err);
});
});
});
});
1 change: 1 addition & 0 deletions test/localTemplateOptions/data-access-template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{_templateOptions.data.greeting}} {{_templateOptions.data.firstName}} {{_templateOptions.data.lastName}}
1 change: 1 addition & 0 deletions test/localTemplateOptions/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{@greeting}} {{@firstName}} {{@lastName}}

0 comments on commit ebb6112

Please sign in to comment.