diff options
author | kpdecker <kpdecker@gmail.com> | 2013-05-31 13:12:06 -0400 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2013-05-31 13:12:06 -0400 |
commit | 16fd601a5caab026f26fa8adb63e90e22d9f5d77 (patch) | |
tree | da41e66239e486270dd1178e51611211f58dc4d0 | |
parent | ef062adcc24ea2c1c617cbe54de6d8982d739a40 (diff) | |
download | handlebars.js-16fd601a5caab026f26fa8adb63e90e22d9f5d77.zip handlebars.js-16fd601a5caab026f26fa8adb63e90e22d9f5d77.tar.gz handlebars.js-16fd601a5caab026f26fa8adb63e90e22d9f5d77.tar.bz2 |
Merge global and passed helpers and partials
-rw-r--r-- | dist/handlebars.js | 20 | ||||
-rw-r--r-- | dist/handlebars.runtime.js | 15 | ||||
-rw-r--r-- | lib/handlebars/base.js | 5 | ||||
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 5 | ||||
-rw-r--r-- | lib/handlebars/runtime.js | 10 | ||||
-rw-r--r-- | release-notes.md | 2 | ||||
-rw-r--r-- | spec/qunit_spec.js | 28 |
7 files changed, 67 insertions, 18 deletions
diff --git a/dist/handlebars.js b/dist/handlebars.js index 418b804..e2c394f 100644 --- a/dist/handlebars.js +++ b/dist/handlebars.js @@ -30,12 +30,13 @@ var Handlebars = {}; // lib/handlebars/base.js Handlebars.VERSION = "1.0.0-rc.4"; -Handlebars.COMPILER_REVISION = 3; +Handlebars.COMPILER_REVISION = 4; Handlebars.REVISION_CHANGES = { 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it 2: '== 1.0.0-rc.3', - 3: '>= 1.0.0-rc.4' + 3: '== 1.0.0-rc.4', + 4: '>= 1.0.0' }; Handlebars.helpers = {}; @@ -1385,8 +1386,9 @@ JavaScriptCompiler.prototype = { if (!this.isChild) { var namespace = this.namespace; - var copies = "helpers = helpers || " + namespace + ".helpers;"; - if (this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; } + + var copies = "helpers = this.merge(helpers, " + namespace + ".helpers);"; + if (this.environment.usePartial) { copies = copies + " partials = this.merge(partials, " + namespace + ".partials);"; } if (this.options.data) { copies = copies + " data = data || {};"; } out.push(copies); } else { @@ -2190,6 +2192,16 @@ Handlebars.VM = { } return programWrapper; }, + merge: function(param, common) { + var ret = param || common; + + if (param && common) { + ret = {}; + Handlebars.Utils.extend(ret, common); + Handlebars.Utils.extend(ret, param); + } + return ret; + }, programWithDepth: Handlebars.VM.programWithDepth, noop: Handlebars.VM.noop, compilerInfo: null diff --git a/dist/handlebars.runtime.js b/dist/handlebars.runtime.js index d11c79d..7e88761 100644 --- a/dist/handlebars.runtime.js +++ b/dist/handlebars.runtime.js @@ -30,12 +30,13 @@ var Handlebars = {}; // lib/handlebars/base.js Handlebars.VERSION = "1.0.0-rc.4"; -Handlebars.COMPILER_REVISION = 3; +Handlebars.COMPILER_REVISION = 4; Handlebars.REVISION_CHANGES = { 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it 2: '== 1.0.0-rc.3', - 3: '>= 1.0.0-rc.4' + 3: '== 1.0.0-rc.4', + 4: '>= 1.0.0' }; Handlebars.helpers = {}; @@ -275,6 +276,16 @@ Handlebars.VM = { } return programWrapper; }, + merge: function(param, common) { + var ret = param || common; + + if (param && common) { + ret = {}; + Handlebars.Utils.extend(ret, common); + Handlebars.Utils.extend(ret, param); + } + return ret; + }, programWithDepth: Handlebars.VM.programWithDepth, noop: Handlebars.VM.noop, compilerInfo: null diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index ad2d9ec..e1cd712 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -7,12 +7,13 @@ var Handlebars = {}; // BEGIN(BROWSER) Handlebars.VERSION = "1.0.0-rc.4"; -Handlebars.COMPILER_REVISION = 3; +Handlebars.COMPILER_REVISION = 4; Handlebars.REVISION_CHANGES = { 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it 2: '== 1.0.0-rc.3', - 3: '>= 1.0.0-rc.4' + 3: '== 1.0.0-rc.4', + 4: '>= 1.0.0' }; Handlebars.helpers = {}; diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index c003ce6..8bb1fc5 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -508,8 +508,9 @@ JavaScriptCompiler.prototype = { if (!this.isChild) { var namespace = this.namespace; - var copies = "helpers = helpers || " + namespace + ".helpers;"; - if (this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; } + + var copies = "helpers = this.merge(helpers, " + namespace + ".helpers);"; + if (this.environment.usePartial) { copies = copies + " partials = this.merge(partials, " + namespace + ".partials);"; } if (this.options.data) { copies = copies + " data = data || {};"; } out.push(copies); } else { diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index d99019d..2e845c4 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -18,6 +18,16 @@ Handlebars.VM = { } return programWrapper; }, + merge: function(param, common) { + var ret = param || common; + + if (param && common) { + ret = {}; + Handlebars.Utils.extend(ret, common); + Handlebars.Utils.extend(ret, param); + } + return ret; + }, programWithDepth: Handlebars.VM.programWithDepth, noop: Handlebars.VM.noop, compilerInfo: null diff --git a/release-notes.md b/release-notes.md index bb44560..477fc22 100644 --- a/release-notes.md +++ b/release-notes.md @@ -9,12 +9,14 @@ - [#519](https://github.com/wycats/handlebars.js/issues/519) - Fix partials with . name ([@jamesgorrie](https://github.com/jamesgorrie)) - [#519](https://github.com/wycats/handlebars.js/issues/519) - Allow ID or strings in partial names - [#437](https://github.com/wycats/handlebars.js/issues/437) - Require matching brace counts in escaped expressions +- Merge passed partials and helpers with global namespace values - Add support for complex ids in @data references - Docs updates Compatibility notes: - The parser is now stricter on `{{{`, requiring that the end token be `}}}`. Templates that do not follow this convention should add the additional brace value. +- Code that relies on global the namespace being muted when custom helpers or partials are passed will need to explicitly pass an `undefined` value for any helpers that should not be available. [Commits](https://github.com/wycats/handlebars.js/compare/v1.0.11...master) diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index ec58570..5d52e44 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -42,14 +42,6 @@ function shouldCompileToWithPartials(string, hashOrArray, partials, expected, me function compileWithPartials(string, hashOrArray, partials) { var template = CompilerContext[partials ? 'compileWithPartial' : 'compile'](string), ary; if(Object.prototype.toString.call(hashOrArray) === "[object Array]") { - var helpers = hashOrArray[1]; - - if(helpers) { - for(var prop in Handlebars.helpers) { - helpers[prop] = helpers[prop] || Handlebars.helpers[prop]; - } - } - ary = []; ary.push(hashOrArray[0]); ary.push({ helpers: hashOrArray[1], partials: hashOrArray[2] }); @@ -500,6 +492,17 @@ test("the helpers hash is available is nested contexts", function() { "helpers hash is available in nested contexts."); }); +test("the helper hash should augment the global hash", function() { + Handlebars.registerHelper('test_helper', function() { return 'found it!'; }); + + shouldCompileTo( + "{{test_helper}} {{#if cruel}}Goodbye {{cruel}} {{world}}!{{/if}}", [ + {cruel: "cruel"}, + {world: function() { return "world!"; }} + ], + "found it! Goodbye cruel world!!"); +}); + test("Multiple global helper registration", function() { var helpers = Handlebars.helpers; try { @@ -592,6 +595,15 @@ test("Partials with slash and point paths", function() { shouldCompileToWithPartials(string, [hash, {}, {'shared/dude.thing':dude}], true, "Dudes: Jeepers", "Partials can use literal with points in paths"); }); +test("Global Partials", function() { + Handlebars.registerPartial('global_test', '{{another_dude}}'); + + var string = "Dudes: {{> shared/dude}} {{> global_test}}"; + var dude = "{{name}}"; + var hash = {name:"Jeepers", another_dude:"Creepers"}; + shouldCompileToWithPartials(string, [hash, {}, {'shared/dude':dude}], true, "Dudes: Jeepers Creepers", "Partials can use globals or passed"); +}); + test("Multiple partial registration", function() { Handlebars.registerPartial({ 'shared/dude': '{{name}}', |