From ba16c76887d0501778aeb3b27a220ba07ab3f439 Mon Sep 17 00:00:00 2001 From: John Butler Date: Thu, 20 Oct 2011 10:33:56 +0100 Subject: [PATCH] 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); }