implement template merging