From a884509c6a79594c0dc27903314445b14f20ddcd Mon Sep 17 00:00:00 2001
From: John Butler
Date: Wed, 19 Oct 2011 22:47:13 +0200
Subject: [PATCH 1/4] Adding support for dot notation
---
mustache.js | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/mustache.js b/mustache.js
index 31566ce..24899c1 100644
--- a/mustache.js
+++ b/mustache.js
@@ -275,6 +275,10 @@ var Mustache = function() {
value = this.context[name];
}
+ if(name.match(/([a-z_]+)\.?/ig)){
+ value = this.walk_context(name, context);
+ }
+
if(typeof value === "function") {
return value.apply(context);
}
@@ -285,6 +289,20 @@ var Mustache = function() {
return "";
},
+ walk_context: function(name, context){
+ var path = name.split('.')
+ var value_context = context;
+ var value = context[path.shift()];
+ while(value != undefined && path.length > 0){
+ value_context = value
+ value = value[path.shift()];
+ }
+ if(typeof value === "function") {
+ return value.apply(value_context);
+ }
+ return value;
+ },
+
// Utility methods
/* includes tag */
@@ -393,4 +411,4 @@ var Mustache = function() {
}
}
});
-}();
+}();
\ No newline at end of file
From 3ffc6b282878676c9a9e1a53e66dd01f66b3e1f8 Mon Sep 17 00:00:00 2001
From: John Butler
Date: Wed, 19 Oct 2011 22:44:11 +0100
Subject: [PATCH 2/4] adding simple test for dot notation support
---
examples/dot_notation.html | 6 ++++++
examples/dot_notation.js | 14 ++++++++++++++
examples/dot_notation.txt | 4 ++++
3 files changed, 24 insertions(+)
create mode 100644 examples/dot_notation.html
create mode 100644 examples/dot_notation.js
create mode 100644 examples/dot_notation.txt
diff --git a/examples/dot_notation.html b/examples/dot_notation.html
new file mode 100644
index 0000000..8cef14a
--- /dev/null
+++ b/examples/dot_notation.html
@@ -0,0 +1,6 @@
+{{name}}
+Price: {{price.currency.symbol}}{{price.value}} {{#price.currency}}{{name}}{{/price.currency}}
+VAT: {{price.currency.symbol}}{{price.vat}}
+{{#in_stock}}
+Available Now
+{{/in_stock}}
\ No newline at end of file
diff --git a/examples/dot_notation.js b/examples/dot_notation.js
new file mode 100644
index 0000000..707174c
--- /dev/null
+++ b/examples/dot_notation.js
@@ -0,0 +1,14 @@
+var dot_notation = {
+ name: "A Book",
+ price:{
+ value: 200,
+ vat: function() {
+ return this.value * 0.2;
+ },
+ currency: {
+ symbol: '€',
+ name: 'Euro'
+ }
+ },
+ in_stock: true
+};
\ No newline at end of file
diff --git a/examples/dot_notation.txt b/examples/dot_notation.txt
new file mode 100644
index 0000000..304ba38
--- /dev/null
+++ b/examples/dot_notation.txt
@@ -0,0 +1,4 @@
+A Book
+Price: €200 Euro
+VAT: €40
+Available Now
From ba16c76887d0501778aeb3b27a220ba07ab3f439 Mon Sep 17 00:00:00 2001
From: John Butler
Date: Thu, 20 Oct 2011 10:33:56 +0100
Subject: [PATCH 3/4] dot notation support added - all tests passing - updated
test to include check that it doesn't break IMPLICIT_OPERATOR - added fix to
ensure that you can access global context from within 'dot notatated' blocks
---
mustache.js | 29 +++++++++++++++++------------
1 file changed, 17 insertions(+), 12 deletions(-)
diff --git a/mustache.js b/mustache.js
index 24899c1..21d5726 100644
--- a/mustache.js
+++ b/mustache.js
@@ -269,14 +269,17 @@ var Mustache = function() {
}
var value;
- if(is_kinda_truthy(context[name])) {
- value = context[name];
- } else if(is_kinda_truthy(this.context[name])) {
- value = this.context[name];
- }
-
- if(name.match(/([a-z_]+)\.?/ig)){
- value = this.walk_context(name, context);
+
+ // check for dot notation eg. foo.bar
+ if(name.match(/([a-z_]+)\./ig)){
+ value = is_kinda_truthy(this.walk_context(name, context));
+ }
+ else{
+ if(is_kinda_truthy(context[name])) {
+ value = context[name];
+ } else if(is_kinda_truthy(this.context[name])) {
+ value = this.context[name];
+ }
}
if(typeof value === "function") {
@@ -290,13 +293,15 @@ var Mustache = function() {
},
walk_context: function(name, context){
- var path = name.split('.')
- var value_context = context;
- var value = context[path.shift()];
+ var path = name.split('.');
+ // if the var doesn't exist in current context, check the top level context
+ var value_context = (context[path[0]] != undefined) ? context : this.context;
+ var value = value_context[path.shift()];
while(value != undefined && path.length > 0){
- value_context = value
+ value_context = value;
value = value[path.shift()];
}
+ // if the value is a function, call it, binding the correct context
if(typeof value === "function") {
return value.apply(value_context);
}
From a6f913a92ab5d58f16f62f1216513e2afd03d8d2 Mon Sep 17 00:00:00 2001
From: John Butler
Date: Thu, 20 Oct 2011 10:36:54 +0100
Subject: [PATCH 4/4] it would be handy if I committed the tests!
---
examples/dot_notation.html | 8 +++-----
examples/dot_notation.js | 6 +++++-
examples/dot_notation.txt | 4 ++--
3 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/examples/dot_notation.html b/examples/dot_notation.html
index 8cef14a..749ca3d 100644
--- a/examples/dot_notation.html
+++ b/examples/dot_notation.html
@@ -1,6 +1,4 @@
{{name}}
-Price: {{price.currency.symbol}}{{price.value}} {{#price.currency}}{{name}}{{/price.currency}}
-VAT: {{price.currency.symbol}}{{price.vat}}
-{{#in_stock}}
-Available Now
-{{/in_stock}}
\ No newline at end of file
+Authors:
{{#authors}}- {{.}}
{{/authors}}
+Price: {{price.currency.symbol}}{{price.value}} {{#price.currency}}{{name}} {{availability.text}}{{/price.currency}}
+VAT: {{price.currency.symbol}}{{price.vat}}
\ No newline at end of file
diff --git a/examples/dot_notation.js b/examples/dot_notation.js
index 707174c..f41fafb 100644
--- a/examples/dot_notation.js
+++ b/examples/dot_notation.js
@@ -1,5 +1,6 @@
var dot_notation = {
name: "A Book",
+ authors: ["John Power", "Jamie Walsh"],
price:{
value: 200,
vat: function() {
@@ -10,5 +11,8 @@ var dot_notation = {
name: 'Euro'
}
},
- in_stock: true
+ availability:{
+ status: true,
+ text: "In Stock"
+ }
};
\ No newline at end of file
diff --git a/examples/dot_notation.txt b/examples/dot_notation.txt
index 304ba38..770e4ce 100644
--- a/examples/dot_notation.txt
+++ b/examples/dot_notation.txt
@@ -1,4 +1,4 @@
A Book
-Price: €200 Euro
+Authors:
+Price: €200 Euro In Stock
VAT: €40
-Available Now