new foreach in template fixes #667 submitted by pteague