Fix #15053 and #22232 - Avoid recursion when extending templates of the same name

This pull request isn't ready to merge yet, but I'd like to get feedback and thoughts on the approach.

To supplement the diff, I added a file called PROGRESS.md that explains the rationale behind this approach. Hopefully it's a help to a reviewer.