Skip to content

Commit ba16c76

Browse files
committed
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
1 parent 3ffc6b2 commit ba16c76

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

mustache.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,17 @@ var Mustache = function() {
269269
}
270270

271271
var value;
272-
if(is_kinda_truthy(context[name])) {
273-
value = context[name];
274-
} else if(is_kinda_truthy(this.context[name])) {
275-
value = this.context[name];
276-
}
277-
278-
if(name.match(/([a-z_]+)\.?/ig)){
279-
value = this.walk_context(name, context);
272+
273+
// check for dot notation eg. foo.bar
274+
if(name.match(/([a-z_]+)\./ig)){
275+
value = is_kinda_truthy(this.walk_context(name, context));
276+
}
277+
else{
278+
if(is_kinda_truthy(context[name])) {
279+
value = context[name];
280+
} else if(is_kinda_truthy(this.context[name])) {
281+
value = this.context[name];
282+
}
280283
}
281284

282285
if(typeof value === "function") {
@@ -290,13 +293,15 @@ var Mustache = function() {
290293
},
291294

292295
walk_context: function(name, context){
293-
var path = name.split('.')
294-
var value_context = context;
295-
var value = context[path.shift()];
296+
var path = name.split('.');
297+
// if the var doesn't exist in current context, check the top level context
298+
var value_context = (context[path[0]] != undefined) ? context : this.context;
299+
var value = value_context[path.shift()];
296300
while(value != undefined && path.length > 0){
297-
value_context = value
301+
value_context = value;
298302
value = value[path.shift()];
299303
}
304+
// if the value is a function, call it, binding the correct context
300305
if(typeof value === "function") {
301306
return value.apply(value_context);
302307
}

0 commit comments

Comments
 (0)