Skip to content

Commit

Permalink
Merge pull request #127 from zzen/patch-2
Browse files Browse the repository at this point in the history
Fixed :else in section when iterating over empty array.
  • Loading branch information
vybs committed Sep 10, 2012
2 parents c5983ac + de0ce85 commit 00d3d2d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,18 @@ Chunk.prototype.section = function(elem, context, bodies, params) {
if (dust.isArray(elem)) {
if (body) {
var len = elem.length, chunk = this;
context.stack.head['$len'] = len;
for (var i=0; i<len; i++) {
context.stack.head['$idx'] = i;
chunk = body(chunk, context.push(elem[i], i, len));
if (len > 0) {
context.stack.head['$len'] = len;
for (var i=0; i<len; i++) {
context.stack.head['$idx'] = i;
chunk = body(chunk, context.push(elem[i], i, len));
}
context.stack.head['$idx'] = undefined;
context.stack.head['$len'] = undefined;
return chunk;
} else if (skip) {
return skip(this, context);
}
context.stack.head['$idx'] = undefined;
context.stack.head['$len'] = undefined;
return chunk;
}
} else if (elem === true) {
if (body) return body(this, context);
Expand Down
7 changes: 7 additions & 0 deletions test/jasmine-test/spec/grammarTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ var grammarTests = [
expected: "foo, not bar!",
message:"should test the else block"
},
{
name: "empty_else_block",
source: "{#foo}full foo{:else}empty foo{/foo}",
context: { foo: []},
expected: "empty foo",
message: "should test else block when array empty"
},
{
name: "conditional",
source: "{?tags}\n" +
Expand Down
1 change: 1 addition & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ <h1>{dust}</h1>
<option value="25">child_template</option>
<option value="26">recursion</option>
<option value="27">comments</option>
<option value="28">empty_else_block</option>
</select></span> or write your own:
</p>
<textarea id="input-source" class="console"></textarea>
Expand Down

0 comments on commit 00d3d2d

Please sign in to comment.