Browse Source

Adding support for dot notation

tags/0.4.0
John Butler 14 years ago
parent
commit
a884509c6a
1 changed files with 19 additions and 1 deletions
  1. +19
    -1
      mustache.js

+ 19
- 1
mustache.js View File

@@ -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() {
}
}
});
}();
}();

Loading…
Cancel
Save