Fixing var scope in template