diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 03f41ef56..a244906f4 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -121,6 +121,16 @@ Handlebars.registerHelper('log', function(context) { Handlebars.log(context); }); +Handlebars.registerHelper('include', function(name, options) { + var context = {}, + mergeContext = function(obj) { + for(var k in obj)context[k]=obj[k]; + }; + mergeContext(this); + mergeContext(options.hash); + return new Handlebars.SafeString(Handlebars.VM.invokePartial(Handlebars.partials[name], name, context, {}, Handlebars.partials)); +}); + }(this.Handlebars)); // END(BROWSER) diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 7578dd2d7..fe2ef7627 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -55,7 +55,8 @@ Handlebars.JavaScriptCompiler = function() {}; 'if': true, 'unless': true, 'with': true, - 'log': true + 'log': true, + 'include': true }; if (knownHelpers) { for (var name in knownHelpers) { diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index 5c9425396..760112235 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -730,6 +730,15 @@ test("each with @index", function() { equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used"); }); +test("include partials with extendend contexts", function() { + var + string = '{{#each dudes}}{{include "dude" greeting=..}} {{/each}}', + hash = {hello: "Hi", dudes: [{name: "Yehuda", url: "http://yehuda"}, {name: "Alan", url: "http://alan"}]}, + partial = "{{greeting.hello}}, {{name}}!"; + Handlebars.registerPartial('dude', partial); + shouldCompileToWithPartials(string, [hash], true, "Hi, Yehuda! Hi, Alan! "); +}); + test("log", function() { var string = "{{log blah}}"; var hash = { blah: "whee" };