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

add support for GH-292, support the template name in nested partials #306

Merged
merged 2 commits into from
Jul 17, 2013
Merged
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
8 changes: 6 additions & 2 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,9 @@ Chunk.prototype.block = function(elem, context, bodies) {

Chunk.prototype.partial = function(elem, context, params) {
var partialContext;
context.global.__templates__.push(elem);
if(context.global && context.global.__templates__){
context.global.__templates__.push(elem);
}
if (params){
//put the params context second to match what section does. {.} matches the current context without parameters
// start with an empty context
Expand All @@ -517,7 +519,9 @@ Chunk.prototype.partial = function(elem, context, params) {
else {
partialChunk = dust.load(elem, this, partialContext);
}
context.global.__templates__.pop();
if(context.global && context.global.__templates__) {
context.global.__templates__.pop();
}
return partialChunk;
};

Expand Down
75 changes: 74 additions & 1 deletion test/jasmine-test/spec/coreTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ var coreTests = [
expected: "apps/test/foo.tl&v=0.1",
message: "should render the template name with paths"
},
{
name: "makeBase_missing_global",
source: '{#helper}{/helper}',
context: { "helper": function(chunk, context, bodies, params)
{
var newContext = {};
return bodies.block(chunk, dust.makeBase(newContext));
}
},
expected: "",
message: "should render the helper with missing global context"
},
{
name: "reference",
source: "{?one}{one}{/one}",
Expand Down Expand Up @@ -755,6 +767,27 @@ var coreTests = [
context: { name: "Mick", count: 30 },
expected: "Hello Mick! You have 30 new messages.",
message: "should test a blocks with no defaults"
},
{
name: "partial_print_name",
source: "{#helper}{/helper}",
context: {},
expected: "",
message: "should test a template with missing helper"
},
{
name: "nested_partial_print_name",
source: "{>partial_print_name/}",
context: {},
expected: "",
message: "should test a template with missing helper"
},
{
name: "nested_nested_partial_print_name",
source: "{>nested_partial_print_name/}",
context: {},
expected: "",
message: "should test nested partial"
}
]
},
Expand Down Expand Up @@ -858,7 +891,47 @@ var coreTests = [
context: { n: "Mick", c: 30 },
expected: "Hello Mick! You have 30 new messages.",
message: "should test partial with no blocks, ignore the override inline partials"
}
},
{
name: "partial prints the current template name",
source: '{>partial_print_name/}',
context: { "helper": function(chunk, context, bodies, params)
{
var len = Object.keys(context.global.__templates__).length;
// top of the current stack
currentTemplateName = context.global.__templates__[len - 1];
return chunk.write(currentTemplateName);
}
},
expected: "partial_print_name",
message: "should print the current template name"
},
{
name: "nested partial prints the current template name",
source: '{>nested_partial_print_name/}',
context: { "helper": function(chunk, context, bodies, params)
{
var len = Object.keys(context.global.__templates__).length;
// top of the current stack
currentTemplateName = context.global.__templates__[len - 1];
return chunk.write(currentTemplateName);
}
},
expected: "partial_print_name",
message: "should print the current template name"
},
{
name: "partial with makeBase_missing_global",
source: '{#helper template="partial"}{/helper}',
context: { "helper": function(chunk, context, bodies, params)
{
var newContext = {};
return chunk.partial(params.template, dust.makeBase(newContext));
}
},
expected: "Hello ! You have new messages.",
message: "should render the helper with missing global context"
},
]
},
{
Expand Down