Skip to content

Commit 25d1fe4

Browse files
author
Leonhardt Wille
committed
added 'include' helper from handlebars-lang#182
1 parent 39832c0 commit 25d1fe4

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

lib/handlebars/base.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ Handlebars.registerHelper('log', function(context) {
121121
Handlebars.log(context);
122122
});
123123

124+
Handlebars.registerHelper('include', function(name, options) {
125+
var context = {},
126+
mergeContext = function(obj) {
127+
for(var k in obj)context[k]=obj[k];
128+
};
129+
mergeContext(this);
130+
mergeContext(options.hash);
131+
return new Handlebars.SafeString(Handlebars.VM.invokePartial(Handlebars.partials[name], name, context, {}, Handlebars.partials));
132+
});
133+
124134
}(this.Handlebars));
125135

126136
// END(BROWSER)

lib/handlebars/compiler/compiler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ Handlebars.JavaScriptCompiler = function() {};
5555
'if': true,
5656
'unless': true,
5757
'with': true,
58-
'log': true
58+
'log': true,
59+
'include': true
5960
};
6061
if (knownHelpers) {
6162
for (var name in knownHelpers) {

spec/qunit_spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,15 @@ test("each with @index", function() {
730730
equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used");
731731
});
732732

733+
test("include partials with extendend contexts", function() {
734+
var
735+
string = '{{#each dudes}}{{include "dude" greeting=..}} {{/each}}',
736+
hash = {hello: "Hi", dudes: [{name: "Yehuda", url: "http://yehuda"}, {name: "Alan", url: "http://alan"}]},
737+
partial = "{{greeting.hello}}, {{name}}!";
738+
Handlebars.registerPartial('dude', partial);
739+
shouldCompileToWithPartials(string, [hash], true, "Hi, Yehuda! Hi, Alan! ");
740+
});
741+
733742
test("log", function() {
734743
var string = "{{log blah}}";
735744
var hash = { blah: "whee" };

0 commit comments

Comments
 (0)