Permalink
Browse files

Loop vars no longer left in context. Closes #22

  • Loading branch information...
1 parent 90d6fa1 commit 5c0d6f523e57fb8d9feeaadbc488eec354e9329b @gsf committed Dec 31, 2013
Showing with 17 additions and 7 deletions.
  1. +1 −1 dist/whiskers.min.js
  2. +4 −4 lib/whiskers.js
  3. +6 −1 test/browser/test-for.js
  4. +6 −1 test/server/test-for.js
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -44,7 +44,7 @@
if (block && !block.elsed) {
block.elsed = true;
if (block.statement == 'if') return '\'}else{b+=\'';
- if (block.statement == 'for') return '\'}if(!g(c,\''+block.key+'\')){b+=\'';
+ if (block.statement == 'for') return '\'}if(!g(c,\''+block.forKey+'\')){b+=\'';
}
console.warn('extra {else} ignored');
return '';
@@ -56,9 +56,9 @@
if (partial) return '\'+r(g(c,\''+partial+'\'),c)+\'';
// {for foo in bar}
if (forKey) {
- stack.push({statement:'for', key:forKey});
safeIterVar = iterVar.replace('-', '__');
- return '\';var '+safeIterVar+'A=g(c,\''+forKey+'\');for(var '+safeIterVar+'I=0;'+safeIterVar+'I<'+safeIterVar+'A.length;'+safeIterVar+'I++){c[\''+iterVar+'\']='+safeIterVar+'A['+safeIterVar+'I];b+=\'';
+ stack.push({statement:'for', forKey:forKey, iterVar:iterVar, safeIterVar:safeIterVar});
+ return '\';var __'+safeIterVar+'=g(c,\''+iterVar+'\');var '+safeIterVar+'A=g(c,\''+forKey+'\');for(var '+safeIterVar+'I=0;'+safeIterVar+'I<'+safeIterVar+'A.length;'+safeIterVar+'I++){c[\''+iterVar+'\']='+safeIterVar+'A['+safeIterVar+'I];b+=\'';
}
// {if foo} or {if not foo}
if (ifKey) {
@@ -70,7 +70,7 @@
block = stack[stack.length-1];
if (block && block.statement == closeStatement) {
stack.pop();
- return '\'}b+=\'';
+ return '\'}'+(block.statement == 'for' ? 'c[\''+block.iterVar+'\']=__'+block.safeIterVar+';' : '')+'b+=\'';
}
console.warn('extra {/'+closeStatement+'} ignored');
return '';
View
@@ -1,6 +1,6 @@
// test "for" tag
-test('for', 16, function() {
+test('for', 18, function() {
var template = '{for x in arr}{x}{/for}';
equal(whiskers.render(template, {}), '');
@@ -52,6 +52,11 @@ test('for', 16, function() {
template = '{for x-y in foo-bar.arr-var}{x-y}{else}blah{/for}';
+ equal(whiskers.render(template, {}), 'blah');
+
context = {'foo-bar': {'arr-var': [1,2,3]}};
equal(whiskers.render(template, context), '123');
+
+ // loop var used above shouldn't populate the general context #22
+ equal(context['x-y'], '');
});
View
@@ -4,7 +4,7 @@ var common = require('./common');
var assert = common.assert;
var whiskers = common.whiskers;
-common.expected = 16;
+common.expected = 18;
var template = '{for x in arr}{x}{/for}';
@@ -57,5 +57,10 @@ assert.equal(whiskers.render(template, context), '123');
template = '{for x-y in foo-bar.arr-var}{x-y}{else}blah{/for}';
+assert.equal(whiskers.render(template, {}), 'blah');
+
context = {'foo-bar': {'arr-var': [1,2,3]}};
assert.equal(whiskers.render(template, context), '123');
+
+// loop var used above shouldn't populate the general context #22
+assert.equal(context['x-y'], '');

0 comments on commit 5c0d6f5

Please sign in to comment.