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

Exposed current Context to lambdas via Mustache.currentContext #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions mustache.js
Expand Up @@ -22,6 +22,7 @@ var Mustache;
exports.name = "mustache.js";
exports.version = "0.7.0";
exports.tags = ["{{", "}}"];
exports.currentContext = null; // behavior is defined only when dereferenced from within a lambda call

exports.Scanner = Scanner;
exports.Context = Context;
Expand Down Expand Up @@ -125,9 +126,11 @@ var Mustache;
return match;
};

function Context(view, parent) {
function Context(view, parent, index, array) {
this.view = view;
this.parent = parent;
if (!!index) this.index = index;
this.array = array;
this.clearCache();
}

Expand All @@ -139,8 +142,8 @@ var Mustache;
this._cache = {};
};

Context.prototype.push = function (view) {
return new Context(view, this);
Context.prototype.push = function (view, index, array) {
return new Context(view, this, index, array);
};

Context.prototype.lookup = function (name) {
Expand Down Expand Up @@ -177,12 +180,15 @@ var Mustache;
}

if (typeof value === "function") {
exports.currentContext = this;
value = value.call(this.view);
}

return value;
};

Context.prototype.index = 0;

function Writer() {
this.clearCache();
}
Expand Down Expand Up @@ -241,7 +247,7 @@ var Mustache;
var buffer = "";

for (var i = 0, len = value.length; i < len; ++i) {
buffer += callback(this, context.push(value[i]));
buffer += callback(this, context.push(value[i], i, value));
}

return buffer;
Expand All @@ -254,6 +260,7 @@ var Mustache;
return self.render(template, context);
};

exports.currentContext = context;
return value.call(context.view, text, scopedRender) || "";
default:
if (value) {
Expand Down Expand Up @@ -290,6 +297,7 @@ var Mustache;
var value = context.lookup(name);

if (typeof value === "function") {
exports.currentContext = context;
value = value.call(context.view);
}

Expand Down
7 changes: 7 additions & 0 deletions test/_files/current_context_last.js
@@ -0,0 +1,7 @@
({
items:[1,2,3],
last: function() {
var ctx = Mustache.currentContext;
return (ctx.array.length === ctx.index+1);
}
})
2 changes: 2 additions & 0 deletions test/_files/current_context_last.mustache
@@ -0,0 +1,2 @@
{{#items}}{{.}}{{^last}}, {{/last}}{{/items}}

2 changes: 2 additions & 0 deletions test/_files/current_context_last.txt
@@ -0,0 +1,2 @@
1, 2, 3

7 changes: 7 additions & 0 deletions test/_files/current_context_oddeven.js
@@ -0,0 +1,7 @@
({
rows: [1,2,3],
oddeven: function() {
var ctx = Mustache.currentContext;
return (ctx.index % 2) ? 'even' : 'odd';
}
})
5 changes: 5 additions & 0 deletions test/_files/current_context_oddeven.mustache
@@ -0,0 +1,5 @@
<table>
{{#rows}}
<tr class="{{oddeven}}"><td>{{.}}</td></tr>
{{/rows}}
</table>
5 changes: 5 additions & 0 deletions test/_files/current_context_oddeven.txt
@@ -0,0 +1,5 @@
<table>
<tr class="odd"><td>1</td></tr>
<tr class="even"><td>2</td></tr>
<tr class="odd"><td>3</td></tr>
</table>