Fixed template variable scope issue