Prevent maximum recursion depth exceeded in templates

This can happen when  `django.template.loaders.cached.Loader` is used 
and we have recursion logic in templates.