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

GH-292 support template name in nested partials, need a stack to store the current name #304

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
21 changes: 14 additions & 7 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ Context.wrap = function(context, name) {
return context;
}
var global= {};
global.__template_name__ = name;
global.__templates__ = [];
global.__templates__.push(name);
return new Context(new Stack(context), global);
};

Expand Down Expand Up @@ -490,6 +491,7 @@ Chunk.prototype.block = function(elem, context, bodies) {

Chunk.prototype.partial = function(elem, context, params) {
var partialContext;
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 @@ -506,12 +508,17 @@ Chunk.prototype.partial = function(elem, context, params) {
} else {
partialContext = context;
}
if (typeof elem === "function") {
return this.capture(elem, partialContext, function(name, chunk) {
dust.load(name, chunk, partialContext).end();
});
}
return dust.load(elem, this, partialContext);
var partialChunk;
if (typeof elem === "function") {
partialChunk = this.capture(elem, partialContext, function(name, chunk) {
dust.load(name, chunk, partialContext).end();
});
}
else {
partialChunk = dust.load(elem, this, partialContext);
}
context.global.__templates__.pop();
return partialChunk;
};

Chunk.prototype.helper = function(name, context, bodies, params) {
Expand Down
4 changes: 2 additions & 2 deletions src/dust.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ identifier "identifier"
/ k:key { var arr = ["key", k]; arr.text = k; return arr; }

number "number"
= n:(frac / integer) { return ['literal', n]; }
= n:(float / integer) { return ['literal', n]; }

frac "frac"
float "float"
= l:integer "." r:integer+ { return parseFloat(l + "." + r.join('')); }

integer "integer"
Expand Down
28 changes: 21 additions & 7 deletions test/jasmine-test/spec/coreTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,31 @@ var coreTests = [
expected: "Hello World!",
message: "should test basic text rendering"
},
{
{
name: "global_template",
source: '{#helper foo="bar" boo="boo"} {/helper}',
context: { "helper": function(chunk, context, bodies, params) { return chunk.write(context.global.__template_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: "global_template",
message: "should render the template name"
},
{
name: "apps/test/foo.tl&v=0.1",
source: '{#helper foo="bar" boo="boo" template="tl/apps/test"} {/helper}',
context: { "helper": function(chunk, context, bodies, params) { return chunk.write(context.global.__template_name__); } },
},
{
name: "apps/test/foo.tl&v=0.1",
source: '{#helper foo="bar" boo="boo" template="tl/apps/test"} {/helper}',
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: "apps/test/foo.tl&v=0.1",
message: "should render the template name with paths"
},
Expand Down