Fix bugs for templates