Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Added piped helpers
  • Loading branch information
techhead committed Oct 1, 2012
commit bca914c64e0fa68c4c1f13f4bb2f54c8a08151da
24 changes: 19 additions & 5 deletions mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Mustache;
var exports = {};

exports.name = "mustache.js";
exports.version = "0.7.0";
exports.version = "0.7.1";
exports.tags = ["{{", "}}"];

exports.Scanner = Scanner;
Expand All @@ -29,6 +29,7 @@ var Mustache;

var whiteRe = /\s*/;
var spaceRe = /\s+/;
var spaceReg = /\s+/g;
var nonSpaceRe = /\S/;
var eqRe = /\s*=/;
var curlyRe = /\s*\}/;
Expand Down Expand Up @@ -143,10 +144,19 @@ var Mustache;
return new Context(view, this);
};

Context.prototype.lookup = function (name) {
Context.prototype.lookup = function (name, view) {
var value = this._cache[name];

if (!value) {

if (!view && name.indexOf("|") > 0) {
var names = name.split("|");
for (var i=0,len=names.length; i<len; i++) {
value = this.lookup(names[i], value);
}
return value;
}

if (name === ".") {
value = this.view;
} else {
Expand Down Expand Up @@ -177,7 +187,7 @@ var Mustache;
}

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

return value;
Expand Down Expand Up @@ -545,8 +555,6 @@ var Mustache;
throw new Error("Unclosed tag at " + scanner.pos);
}

tokens.push([type, value, start, scanner.pos]);

if (type === "name" || type === "{" || type === "&") {
nonSpace = true;
}
Expand All @@ -555,7 +563,13 @@ var Mustache;
if (type === "=") {
tags = value.split(spaceRe);
tagRes = escapeTags(tags);
} else if (type !== "!") { // If not a comment
// The tag's content MUST be a non-whitespace character sequence NOT containing
// the current closing delimiter.
value = value.replace(spaceReg,'');
}

tokens.push([type, value, start, scanner.pos]);
}

squashTokens(tokens);
Expand Down
6 changes: 6 additions & 0 deletions test/_files/piped_helpers_date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
({
timestamp: 1,
iso8601Date: function() {
return new Date(this).toISOString();
}
})
1 change: 1 addition & 0 deletions test/_files/piped_helpers_date.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{timestamp | iso8601Date}}
1 change: 1 addition & 0 deletions test/_files/piped_helpers_date.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1970-01-01T00:00:00.001Z
10 changes: 10 additions & 0 deletions test/_files/piped_helpers_sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
({
numbers: [1,2,3,4],
sum: function() {
var total = 0;
for (var i=0,len=this.length; i<len; i++) {
total += this[i];
}
return total;
}
})
1 change: 1 addition & 0 deletions test/_files/piped_helpers_sum.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{numbers|sum}}
1 change: 1 addition & 0 deletions test/_files/piped_helpers_sum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
10 changes: 10 additions & 0 deletions test/parse_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ var Mustache = require('./../mustache');
var expectations = {
'{{hi}}' : [ [ 'name', 'hi', 0, 6 ] ],
'{{hi.world}}' : [ [ 'name', 'hi.world', 0, 12 ] ],
/*
* This is not the assumption that I would have made based on the spec.
* https://github.com/mustache/spec/blob/master/specs/interpolation.yml
*
* The tag's content MUST be a non-whitespace character sequence NOT containing
* the current closing delimiter.
*
* See corrected version below.
'{{hi . world}}' : [ [ 'name', 'hi . world', 0, 14 ] ],
*/
'{{hi . world}}' : [ [ 'name', 'hi.world', 0, 14 ] ],
'{{ hi}}' : [ [ 'name', 'hi', 0, 7 ] ],
'{{hi }}' : [ [ 'name', 'hi', 0, 7 ] ],
'{{ hi }}' : [ [ 'name', 'hi', 0, 8 ] ],
Expand Down