From e88ceb87ec2f16d79a9f0ca4c125069b6c16bcbc Mon Sep 17 00:00:00 2001 From: Seth Kinast Date: Thu, 16 Apr 2015 17:02:49 -0700 Subject: [PATCH] Resolve dynamic partial names via original context When scoping a partial with a dynamic name to a context like this: {>"{partial}":context /} The evaluation of the partial name was done in `context` instead of the current scope. Fixes #262 Closes #261 --- lib/compiler.js | 2 +- lib/dust.js | 18 +++++++++--------- test/jasmine-test/spec/coreTests.js | 18 ++++++++++++++++-- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 038c3874..883e8998 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -337,7 +337,7 @@ partial: function(context, node) { return '.p(' + compiler.compileNode(context, node[1]) + - ',' + compiler.compileNode(context, node[2]) + + ',ctx,' + compiler.compileNode(context, node[2]) + ',' + compiler.compileNode(context, node[3]) + ')'; }, diff --git a/lib/dust.js b/lib/dust.js index 94ada69a..ad3a9272 100644 --- a/lib/dust.js +++ b/lib/dust.js @@ -833,26 +833,26 @@ return this; }; - Chunk.prototype.partial = function(elem, context, params) { + Chunk.prototype.partial = function(elem, context, partialContext, params) { var head; if (!dust.isEmptyObject(params)) { - context = context.clone(); - head = context.pop(); - context = context.push(params) - .push(head); + partialContext = partialContext.clone(); + head = partialContext.pop(); + partialContext = partialContext.push(params) + .push(head); } if (dust.isTemplateFn(elem)) { // The eventual result of evaluating `elem` is a partial name // Load the partial after getting its name and end the async chunk return this.capture(elem, context, function(name, chunk) { - context.templateName = name; - load(name, chunk, context).end(); + partialContext.templateName = name; + load(name, chunk, partialContext).end(); }); } else { - context.templateName = elem; - return load(elem, this, context); + partialContext.templateName = elem; + return load(elem, this, partialContext); } }; diff --git a/test/jasmine-test/spec/coreTests.js b/test/jasmine-test/spec/coreTests.js index 4fe07eff..0348c119 100755 --- a/test/jasmine-test/spec/coreTests.js +++ b/test/jasmine-test/spec/coreTests.js @@ -1367,6 +1367,20 @@ var coreTests = [ expected: "Hello Joe! You have 30 new messages.", message: "should test partial with literal inline param and context. Fallback values for name or count are undefined" }, + { + name: "partial with dynamic name and context", + source: '{>"{partialName}":me /}', + context: { partialName: "partial", me: { name: "Mick", count: 30 }}, + expected: "Hello Mick! You have 30 new messages.", + message: "should test partial with dynamic name and a context" + }, + { + name: "partial with dynamic name and context and inline params", + source: '{>"{partialName}" name=me.name count=me.count /}', + context: { partialName: "partial", me: { name: "Mick", count: 30 }}, + expected: "Hello Mick! You have 30 new messages.", + message: "should test partial with dynamic name and a context" + }, { name: "partial with blocks and inline params", source: '{>partial_with_blocks name=n count="{c}"/}', @@ -1462,8 +1476,8 @@ var coreTests = [ source: '{#helper template="partial"}{/helper}', context: { "helper": function(chunk, context, bodies, params) { - var newContext = {}; - return chunk.partial(params.template, dust.makeBase(newContext)); + var newContext = dust.makeBase({}); + return chunk.partial(params.template, newContext, newContext, {}); } }, expected: "Hello ! You have new messages.",