deduplicate mergeTemplate code