From a8b9000bdcdbd75b4577b221165e4a829c4921ab Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 21 Mar 2019 15:18:43 +0100 Subject: [PATCH 01/11] Added support for express4 in test helpers createLocals --- test/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helpers.js b/test/helpers.js index f97a215..ad2474a 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -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 }; From d159c6b61c822b7c6d76afea93ce06021cb98b0e Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 2 Apr 2019 17:12:15 +0200 Subject: [PATCH 02/11] Installed lodash@4.17.11 --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 21ab6c8..d6797af 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "dependencies": { "handlebars": "4.0.13", "js-beautify": "1.6.8", + "lodash": "4.17.11", "readdirp": "2.1.0" } } From 2f6b93368335b42e20cf0915e6bf6443e0d9bd59 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 21 Mar 2019 15:20:05 +0100 Subject: [PATCH 03/11] Added support for dynamic template options from locals --- lib/hbs.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/hbs.js b/lib/hbs.js index 3705bef..b8a17e4 100644 --- a/lib/hbs.js +++ b/lib/hbs.js @@ -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 }} @@ -480,7 +481,7 @@ ExpressHbs.prototype.___express = function ___express(filename, source, options, var res; try { - res = template(locals, self._options.templateOptions); + res = template(locals, _.merge({}, self._options.templateOptions, locals._templateOptions || {})); } catch (err) { if (err.message) { err.message = '[' + template.__filename + '] ' + err.message; From 876da208cc4d536e366e4b091b0b7580ffc1f8c3 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 21 Mar 2019 15:20:25 +0100 Subject: [PATCH 04/11] Added tests for dynamic template options --- test/dynamicTemplateOptions.js | 67 ++++++++++++++++++++++++ test/dynamicTemplateOptions/template.hbs | 1 + 2 files changed, 68 insertions(+) create mode 100644 test/dynamicTemplateOptions.js create mode 100644 test/dynamicTemplateOptions/template.hbs diff --git a/test/dynamicTemplateOptions.js b/test/dynamicTemplateOptions.js new file mode 100644 index 0000000..5127612 --- /dev/null +++ b/test/dynamicTemplateOptions.js @@ -0,0 +1,67 @@ +'use strict'; +var assert = require('assert'); +var path = require('path'); +var hbs = require('..'); +var H = require('./helpers'); + +describe('dynamic template options', function () { + var dirname = path.join(__dirname, 'dynamicTemplateOptions'); + + 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); + }); + }); + }); + + 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); + }); + }); + }); +}); diff --git a/test/dynamicTemplateOptions/template.hbs b/test/dynamicTemplateOptions/template.hbs new file mode 100644 index 0000000..b841fc1 --- /dev/null +++ b/test/dynamicTemplateOptions/template.hbs @@ -0,0 +1 @@ +{{@greeting}} {{@firstName}} {{@lastName}} From db889d595e93be16749843e4698c87dee8a0b54e Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Mon, 25 Mar 2019 16:00:45 +0100 Subject: [PATCH 05/11] Added tests for removing _templateOptions from locals --- test/dynamicTemplateOptions.js | 52 +++++++++++++++++++ .../data-access-template.hbs | 1 + 2 files changed, 53 insertions(+) create mode 100644 test/dynamicTemplateOptions/data-access-template.hbs diff --git a/test/dynamicTemplateOptions.js b/test/dynamicTemplateOptions.js index 5127612..c7eccaf 100644 --- a/test/dynamicTemplateOptions.js +++ b/test/dynamicTemplateOptions.js @@ -34,6 +34,32 @@ describe('dynamic template options', function () { 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 () { @@ -63,5 +89,31 @@ describe('dynamic template options', function () { 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); + }); + }); }); }); diff --git a/test/dynamicTemplateOptions/data-access-template.hbs b/test/dynamicTemplateOptions/data-access-template.hbs new file mode 100644 index 0000000..5ac95a6 --- /dev/null +++ b/test/dynamicTemplateOptions/data-access-template.hbs @@ -0,0 +1 @@ +{{_templateOptions.data.greeting}} {{_templateOptions.data.firstName}} {{_templateOptions.data.lastName}} From 648662508ba4ac7d737002bf4ec048c494648ae9 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Mon, 25 Mar 2019 16:01:06 +0100 Subject: [PATCH 06/11] Removed _templateOptions from locals --- lib/hbs.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/hbs.js b/lib/hbs.js index b8a17e4..ff63984 100644 --- a/lib/hbs.js +++ b/lib/hbs.js @@ -481,7 +481,9 @@ ExpressHbs.prototype.___express = function ___express(filename, source, options, var res; try { - res = template(locals, _.merge({}, self._options.templateOptions, locals._templateOptions || {})); + var localTemplateOptions = locals._templateOptions || {}; + delete locals._templateOptions; + res = template(locals, _.merge({}, self._options.templateOptions, localTemplateOptions)); } catch (err) { if (err.message) { err.message = '[' + template.__filename + '] ' + err.message; From a03ce689b106280f1e1e22ee378a2be4257c1b56 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Mon, 25 Mar 2019 16:02:42 +0100 Subject: [PATCH 07/11] Updated renderTemplate to clone before modifying This is because we should not be modifing objects that do not belong to us. The locals object belongs to the express application, outside of express-hbs. --- lib/hbs.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/hbs.js b/lib/hbs.js index ff63984..79521a8 100644 --- a/lib/hbs.js +++ b/lib/hbs.js @@ -482,8 +482,9 @@ ExpressHbs.prototype.___express = function ___express(filename, source, options, try { var localTemplateOptions = locals._templateOptions || {}; - delete locals._templateOptions; - res = template(locals, _.merge({}, self._options.templateOptions, localTemplateOptions)); + var localsClone = _.extend({}, locals); + delete localsClone._templateOptions; + res = template(localsClone, _.merge({}, self._options.templateOptions, localTemplateOptions)); } catch (err) { if (err.message) { err.message = '[' + template.__filename + '] ' + err.message; From 33e4e22f7ee1e45c42fb4a33bcaf6b72112d907f Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Mon, 25 Mar 2019 16:09:26 +0100 Subject: [PATCH 08/11] Added getter/setter methods for localTemplateOptions Used the word update here rather than set to match the global template options method --- lib/hbs.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/hbs.js b/lib/hbs.js index 79521a8..9c9f559 100644 --- a/lib/hbs.js +++ b/lib/hbs.js @@ -402,6 +402,14 @@ 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. */ From ef90765d4c390e7079ad0d5eb2b85bf67b1c8429 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Mon, 25 Mar 2019 16:09:59 +0100 Subject: [PATCH 09/11] Updated renderTemplate to use {get,update}LocalTemplateOptions --- lib/hbs.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/hbs.js b/lib/hbs.js index 9c9f559..02032e4 100644 --- a/lib/hbs.js +++ b/lib/hbs.js @@ -489,9 +489,9 @@ ExpressHbs.prototype.___express = function ___express(filename, source, options, var res; try { - var localTemplateOptions = locals._templateOptions || {}; + var localTemplateOptions = self.getLocalTemplateOptions(locals); var localsClone = _.extend({}, locals); - delete localsClone._templateOptions; + self.updateLocalTemplateOptions(localsClone, undefined); res = template(localsClone, _.merge({}, self._options.templateOptions, localTemplateOptions)); } catch (err) { if (err.message) { From ce640d31894fea27c2848c471ef94b6f92fb1688 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Mon, 25 Mar 2019 16:11:05 +0100 Subject: [PATCH 10/11] Added getTemplateOptions method for completeness --- lib/hbs.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/hbs.js b/lib/hbs.js index 02032e4..1e97e70 100644 --- a/lib/hbs.js +++ b/lib/hbs.js @@ -398,6 +398,10 @@ ExpressHbs.prototype.registerAsyncHelper = function(name, fn) { }); }; +ExpressHbs.prototype.getTemplateOptions = function() { + return this._options.templateOptions; +}; + ExpressHbs.prototype.updateTemplateOptions = function(templateOptions) { this._options.templateOptions = templateOptions; }; From 5460a5a2e664fcb7851bfbde25e79493c28f26ee Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Mon, 25 Mar 2019 16:13:06 +0100 Subject: [PATCH 11/11] Renamed dynamic -> local --- test/{dynamicTemplateOptions.js => localTemplateOptions.js} | 4 ++-- .../data-access-template.hbs | 0 .../template.hbs | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename test/{dynamicTemplateOptions.js => localTemplateOptions.js} (96%) rename test/{dynamicTemplateOptions => localTemplateOptions}/data-access-template.hbs (100%) rename test/{dynamicTemplateOptions => localTemplateOptions}/template.hbs (100%) diff --git a/test/dynamicTemplateOptions.js b/test/localTemplateOptions.js similarity index 96% rename from test/dynamicTemplateOptions.js rename to test/localTemplateOptions.js index c7eccaf..08a6552 100644 --- a/test/dynamicTemplateOptions.js +++ b/test/localTemplateOptions.js @@ -4,8 +4,8 @@ var path = require('path'); var hbs = require('..'); var H = require('./helpers'); -describe('dynamic template options', function () { - var dirname = path.join(__dirname, 'dynamicTemplateOptions'); +describe('local template options', function () { + var dirname = path.join(__dirname, 'localTemplateOptions'); describe('express3', function () { diff --git a/test/dynamicTemplateOptions/data-access-template.hbs b/test/localTemplateOptions/data-access-template.hbs similarity index 100% rename from test/dynamicTemplateOptions/data-access-template.hbs rename to test/localTemplateOptions/data-access-template.hbs diff --git a/test/dynamicTemplateOptions/template.hbs b/test/localTemplateOptions/template.hbs similarity index 100% rename from test/dynamicTemplateOptions/template.hbs rename to test/localTemplateOptions/template.hbs