From c4a08e62df197f053893362a5bcb06d467a71139 Mon Sep 17 00:00:00 2001 From: Connor Taylor Date: Fri, 27 Jan 2017 11:12:08 -0500 Subject: [PATCH 1/2] pass context.view to partials function --- mustache.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mustache.js b/mustache.js index 5ffdfe64e..aeb138429 100644 --- a/mustache.js +++ b/mustache.js @@ -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); }; From edbfaaf1ba8bc2828a6567513424dff3571096e4 Mon Sep 17 00:00:00 2001 From: connor Date: Sat, 4 Feb 2017 22:15:49 -0500 Subject: [PATCH 2/2] added test for when partials are retrieved via a function --- test/_files/partial_function.js | 4 ++++ test/_files/partial_function.mustache | 1 + test/_files/partial_function.partial | 1 + test/_files/partial_function.txt | 1 + test/render-test.js | 8 +++++++- 5 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/_files/partial_function.js create mode 100644 test/_files/partial_function.mustache create mode 100644 test/_files/partial_function.partial create mode 100644 test/_files/partial_function.txt diff --git a/test/_files/partial_function.js b/test/_files/partial_function.js new file mode 100644 index 000000000..8ba5d313d --- /dev/null +++ b/test/_files/partial_function.js @@ -0,0 +1,4 @@ +({ + id: 'main', + value: 'foo' +}) \ No newline at end of file diff --git a/test/_files/partial_function.mustache b/test/_files/partial_function.mustache new file mode 100644 index 000000000..7a336fee8 --- /dev/null +++ b/test/_files/partial_function.mustache @@ -0,0 +1 @@ +{{>partial}} \ No newline at end of file diff --git a/test/_files/partial_function.partial b/test/_files/partial_function.partial new file mode 100644 index 000000000..e1f426b1e --- /dev/null +++ b/test/_files/partial_function.partial @@ -0,0 +1 @@ +{{id}}: {{value}} \ No newline at end of file diff --git a/test/_files/partial_function.txt b/test/_files/partial_function.txt new file mode 100644 index 000000000..e7ea4f016 --- /dev/null +++ b/test/_files/partial_function.txt @@ -0,0 +1 @@ +main: foo \ No newline at end of file diff --git a/test/render-test.js b/test/render-test.js index e9d61a9da..e24a0201c 100644 --- a/test/render-test.js +++ b/test/render-test.js @@ -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); }