Skip to content

Commit

Permalink
added 'include' helper from handlebars-lang#182
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonhardt Wille committed Nov 20, 2012
1 parent 39832c0 commit 25d1fe4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/handlebars/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions spec/qunit_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
Expand Down

0 comments on commit 25d1fe4

Please sign in to comment.