Fix bugs in template extension