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

Added piped helpers #261

Open
wants to merge 2 commits 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
17 changes: 14 additions & 3 deletions mustache.js
Expand Up @@ -33,6 +33,8 @@ var Mustache;
var eqRe = /\s*=/;
var curlyRe = /\s*\}/;
var tagRe = /#|\^|\/|>|\{|&|=|!/;
var dotRe = /\s*\.\s*/g;
var pipeRe = /\s*\|\s*/g;

// Workaround for https://issues.apache.org/jira/browse/COUCHDB-577
// See https://github.com/janl/mustache.js/issues/189
Expand Down Expand Up @@ -143,18 +145,27 @@ 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 (typeof view === 'undefined' && name.indexOf("|") > 0) {
var names = name.split(pipeRe);
for (var i=0,len=names.length; i<len; i++) {
value = this.lookup(names[i], value);
}
return value;
}

if (name === ".") {
value = this.view;
} else {
var context = this;

while (context) {
if (name.indexOf(".") > 0) {
var names = name.split("."), i = 0;
var names = name.split(dotRe), i = 0;

value = context.view;

Expand All @@ -177,7 +188,7 @@ var Mustache;
}

if (typeof value === "function") {
value = value.call(this.view);
value = value.call(typeof view === 'undefined' ? this.view : view);
}

return value;
Expand Down
4 changes: 2 additions & 2 deletions test/_files/dot_notation.mustache
Expand Up @@ -5,5 +5,5 @@
<p>VAT: {{{price.currency.symbol}}}{{#price}}{{vat}}{{/price}}</p>
<!-- boring part -->
<h2>Test truthy false values:</h2>
<p>Zero: {{truthy.zero}}</p>
<p>False: {{truthy.notTrue}}</p>
<p>Zero: {{truthy . zero}}</p>
<p>False: {{truthy . notTrue}}</p>
6 changes: 6 additions & 0 deletions test/_files/piped_helpers_date.js
@@ -0,0 +1,6 @@
({
timestamp: 0,
iso8601Date: function() {
return new Date(this).toISOString();
}
})
1 change: 1 addition & 0 deletions test/_files/piped_helpers_date.mustache
@@ -0,0 +1 @@
{{timestamp | iso8601Date}}
1 change: 1 addition & 0 deletions test/_files/piped_helpers_date.txt
@@ -0,0 +1 @@
1970-01-01T00:00:00.000Z
14 changes: 14 additions & 0 deletions test/_files/piped_helpers_sum.js
@@ -0,0 +1,14 @@
({
numbers: [1,2,3,4,-10],
sum: function() {
var total = 0;
for (var i=0,len=this.length; i<len; i++) {
total += this[i];
}
return total;
},
sign: function() {
var num = +this;
return (num < 0) ? num : '+'+num;
}
})
5 changes: 5 additions & 0 deletions test/_files/piped_helpers_sum.mustache
@@ -0,0 +1,5 @@
Numbers:
{{#numbers}}
{{. | sign}}
{{/numbers}}
Sum: {{numbers|sum}}
7 changes: 7 additions & 0 deletions test/_files/piped_helpers_sum.txt
@@ -0,0 +1,7 @@
Numbers:
+1
+2
+3
+4
-10
Sum: 0