From f4c5826ab30584e9d6a97a517311c7afd79599b0 Mon Sep 17 00:00:00 2001 From: Bruno PIERRE Date: Wed, 17 Jul 2013 17:32:02 +0200 Subject: [PATCH] Add a way to acceed to loop data when using block {{#anArray}} {{/anArray}} You may accedd to loop datas with @index @arrayIndex and @length {{@index}} / {{@length}} will output 1 / 12 2 / 12 3 / 12 ... @arrayindex start at 0 instead of 1 --- mustache.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mustache.js b/mustache.js index bee26f965..b34ecb749 100644 --- a/mustache.js +++ b/mustache.js @@ -238,6 +238,17 @@ if (typeof value === 'object') { if (isArray(value)) { + context = context.push({ + '@index':function(){ + return j+1; + }, + '@arrayindex':function(){ + return j; + }, + '@length':function(){ + return jlen; + } + }); for (var j = 0, jlen = value.length; j < jlen; ++j) { buffer += renderTokens(token[4], writer, context.push(value[j]), template); }