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.
Cache loader has been made compatible with self-referenced inheritance.