add Scope data structure for xtemplate. Fixes #519,#518