Skip to content

Commit

Permalink
Merge pull request #335 from prashn64/template-name
Browse files Browse the repository at this point in the history
make context.templateName change into version 2.0.3
  • Loading branch information
jimmyhchan committed Sep 3, 2013
2 parents 72e8996 + 1f62976 commit 072ec58
Show file tree
Hide file tree
Showing 5 changed files with 4,351 additions and 55 deletions.
55 changes: 28 additions & 27 deletions dist/dust-core-2.0.2.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,10 @@ dust.filters = {
jp: function(value) { if (!JSON) { return value; } return JSON.parse(value); }
};

function Context(stack, global, blocks, templateName) {
function Context(stack, global, blocks) {
this.stack = stack;
this.global = global;
this.blocks = blocks;
this.templateName = templateName;
}

dust.makeBase = function(global) {
Expand All @@ -144,7 +143,10 @@ Context.wrap = function(context, name) {
if (context instanceof Context) {
return context;
}
return new Context(new Stack(context), {}, null, name);
var global= {};
global.__templates__ = [];
global.__templates__.push(name);
return new Context(new Stack(context), global);
};

Context.prototype.get = function(key) {
Expand Down Expand Up @@ -176,11 +178,8 @@ Context.prototype.getPath = function(cur, down) {
ctx = ctx[down[i]];
i++;
while (!ctx && !cur){
// i is the count of number of path elements matched. If > 1 then we have a partial match
// and do not continue to search for the rest of the path.
// Note: a falsey value at the end of a matched path also comes here.
// This returns the value or undefined if we just have a partial match.
if (i > 1) return ctx;
//if there was a partial match, don't search further
if (i > 1) return undefined;
if (tail){
ctx = tail.head;
tail = tail.tail;
Expand All @@ -205,11 +204,11 @@ Context.prototype.getPath = function(cur, down) {
};

Context.prototype.push = function(head, idx, len) {
return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks, this.templateName);
return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks);
};

Context.prototype.rebase = function(head) {
return new Context(new Stack(head), this.global, this.blocks, this.templateName);
return new Context(new Stack(head), this.global, this.blocks);
};

Context.prototype.current = function() {
Expand Down Expand Up @@ -242,7 +241,7 @@ Context.prototype.shiftBlocks = function(locals) {
} else {
newBlocks = blocks.concat([locals]);
}
return new Context(this.stack, this.global, newBlocks, this.templateName);
return new Context(this.stack, this.global, newBlocks);
}
return this;
};
Expand Down Expand Up @@ -525,26 +524,25 @@ Chunk.prototype.block = function(elem, context, bodies) {

Chunk.prototype.partial = function(elem, context, params) {
var partialContext;
//put the params context second to match what section does. {.} matches the current context without parameters
// start with an empty context
partialContext = dust.makeBase(context.global);
partialContext.blocks = context.blocks;
if (context.stack && context.stack.tail){
// grab the stack(tail) off of the previous context if we have it
partialContext.stack = context.stack.tail;
}
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
partialContext = dust.makeBase(context.global);
partialContext.blocks = context.blocks;
if (context.stack && context.stack.tail){
// grab the stack(tail) off of the previous context if we have it
partialContext.stack = context.stack.tail;
}
//put params on
partialContext = partialContext.push(params);
//reattach the head
partialContext = partialContext.push(context.stack.head);
} else {
partialContext = context;
}

if(typeof elem === "string") {
partialContext.templateName = elem;
}

//reattach the head
partialContext = partialContext.push(context.stack.head);

var partialChunk;
if (typeof elem === "function") {
partialChunk = this.capture(elem, partialContext, function(name, chunk) {
Expand All @@ -554,6 +552,9 @@ Chunk.prototype.partial = function(elem, context, params) {
else {
partialChunk = dust.load(elem, this, partialContext);
}
if(context.global && context.global.__templates__) {
context.global.__templates__.pop();
}
return partialChunk;
};

Expand Down
Loading

0 comments on commit 072ec58

Please sign in to comment.