Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support foreach.hasnext when iterating objects #81

Merged
merged 1 commit into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions src/compile/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,29 +195,42 @@ module.exports = function(Velocity, utils) {
return '';
}

var len = utils.isArray(_from) ? _from.length : utils.keys(_from).length;

utils.forEach(_from, function(val, i) {
if (utils.isArray(_from)) {
var len = _from.length;
utils.forEach(_from, function(val, i) {
if (this._state.break) {
return;
}
// 构造临时变量
local[_to] = val;
local.foreach = {
count: i + 1,
index: i,
hasNext: i + 1 < len
};
local.velocityCount = i + 1;

if (this._state.break) {
return;
}
// 构造临时变量
local[_to] = val;
// TODO: here, the foreach variable give to local, when _from is not an
// array, count and hasNext would be undefined, also i is not the
// index.
local.foreach = {
count: i + 1,
index: i,
hasNext: i + 1 < len
};
local.velocityCount = i + 1;

this.local[contextId] = local;
ret += this._render(_block, contextId);
this.local[contextId] = local;
ret += this._render(_block, contextId);

}, this);
}, this);
} else {
var len = utils.keys(_from).length;
utils.forEach(utils.keys(_from), function(key, i) {
if (this._state.break) {
return;
}
local[_to] = _from[key];
local.foreach = {
count: i + 1,
index: i,
hasNext: i + 1 < len
};
local.velocityCount = i + 1;
this.local[contextId] = local;
ret += this._render(_block, contextId);
}, this);
}

// if foreach items be an empty array, then this code will shift current
// conditions, but not this._render call, so this will shift parent context
Expand Down
13 changes: 13 additions & 0 deletions tests/foreach.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ describe('Loops', function() {
assert.equal(' name => hanwen ', render(vm, data))
})

it('#foreach with map hasNext', function() {
var vm = '#foreach($product in $products)$product.name#if($foreach.hasNext),#end#end'
var data = {products: {product1: {name: "hanwen1"}, product2: {name: "hanwen2"}, product3: {name: "hanwen3"}}};
assert.equal('hanwen1,hanwen2,hanwen3', render(vm, data))
})

it('#foreach with map keySet', function() {
var vm = '#foreach($key in $products.keySet())' +
' $key => $products.get($key) #end'
Expand Down Expand Up @@ -91,6 +97,13 @@ describe('Loops', function() {
assert.equal(' 1 2 3 4 ', render(vm))
})

it('#break for map', function() {
var vm = '#foreach($item in $map)' +
' #if($foreach.count > 2) #break #end $item #end'
var data = {map: {item1: '1', item2: '2', item3: '3', item4: '4'}}
assert.equal(' 1 2 3 ', render(vm, data))
})

it('foreach for null', function() {
var vm = '#foreach($num in $bar) #end';
assert.equal('', render(vm))
Expand Down