Fixed #15053 -- avoid recursion when extending templates

The `extends` template tag now avoids recursion, making it possible for
templates to inherit from third party app templates with the same name.

Based on the work of Pablo Martín (thanks!)