Add foreach functionality to templates.