From 519f23ff544185c4f252f8b254d1dca82bfc4316 Mon Sep 17 00:00:00 2001 From: Sahab Yazdani Date: Tue, 4 Oct 2011 11:54:39 -0400 Subject: [PATCH] Add dot notation support for interpolation tags --- mustache.js | 45 ++++++++++++++++++++++++++++----------------- test/unit.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 17 deletions(-) diff --git a/mustache.js b/mustache.js index e5f80a3..04e9b00 100644 --- a/mustache.js +++ b/mustache.js @@ -319,6 +319,7 @@ var Mustache = (function(undefined) { } var value; + if (is_kinda_truthy(context[name])) { value = context[name]; } @@ -364,9 +365,32 @@ var Mustache = (function(undefined) { escape = prefix = true; } - var variable = get_variable_name(state, token, prefix, postfix); + var variable = get_variable_name(state, token, prefix, postfix), + implicit_iterator = (state.pragmas['IMPLICIT-ITERATOR'] || {iterator: '.'}).iterator; + state.send_code_func((function(variable, escape) { return function(context, send_func) { - var value = find_in_stack(variable, context); + var variable_components, + i, n, value; + + if ( variable === implicit_iterator ) { // special case for implicit iterator (usually '.') + value = {}; value[implicit_iterator] = context[context.length-1]; + value = find(variable, value); + } else { + variable_components = variable.split('.'); + i = 1; + n = variable_components.length; + + value = find_in_stack(variable_components[0], context); + while (value && i