Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass context.view to partials function #619

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,9 @@
Writer.prototype.renderPartial = function renderPartial (token, context, partials) {
if (!partials) return;

var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
var value = isFunction(partials)
? partials(token[1], context.view)
: partials[token[1]];
if (value != null)
return this.renderTokens(this.parse(value), context, partials, value);
};
Expand Down
4 changes: 4 additions & 0 deletions test/_files/partial_function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
({
id: 'main',
value: 'foo'
})
1 change: 1 addition & 0 deletions test/_files/partial_function.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{>partial}}
1 change: 1 addition & 0 deletions test/_files/partial_function.partial
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{id}}: {{value}}
1 change: 1 addition & 0 deletions test/_files/partial_function.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main: foo
8 changes: 7 additions & 1 deletion test/render-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ describe('Mustache.render', function () {
it('knows how to render ' + test.name, function () {
var output;
if (test.partial) {
output = Mustache.render(test.template, view, { partial: test.partial });
// Ensure partials can be retreived via an object or a function
// See Writer.prototype.renderPartial
var partial = test.name === 'partial_function'
? function (partial, context) { return test.partial }
: { partial: test.partial };

output = Mustache.render(test.template, view, partial);
} else {
output = Mustache.render(test.template, view);
}
Expand Down