def find_template(self, name, context, peeking=False):
<EOL>from django.conf import settings<EOL>import django.template.loaders.app_directories as app_directories<EOL>try:<EOL><INDENT>get_app_template_dirs = app_directories.get_app_template_dirs<EOL>app_template_dirs = get_app_template_dirs('<STR_LIT>')<EOL><DEDENT>except AttributeError:<EOL><INDENT>app_template_dirs = app_directories.app_template_dirs<EOL><DEDENT>try:<EOL><INDENT>find_template_loader = context.template.engine.find_template_loader<EOL>template_dirs = context.template.engine.dirs<EOL>template_loaders = context.template.engine.loaders<EOL><DEDENT>except AttributeError:<EOL><INDENT>from django.template.loader import find_template_loader<EOL>template_dirs = list(settings.TEMPLATE_DIRS)<EOL>template_loaders = settings.TEMPLATE_LOADERS<EOL><DEDENT>context_name = "<STR_LIT>"<EOL>if context_name not in context:<EOL><INDENT>context[context_name] = {}<EOL><DEDENT>if name not in context[context_name]:<EOL><INDENT>all_dirs = template_dirs + list(app_template_dirs)<EOL>context[context_name][name] = list(map(os.path.abspath, all_dirs))<EOL><DEDENT>loaders = []<EOL>for loader_name in template_loaders:<EOL><INDENT>loader = find_template_loader(loader_name)<EOL>loaders.extend(getattr(loader, "<STR_LIT>", [loader]))<EOL><DEDENT>for loader in loaders:<EOL><INDENT>dirs = context[context_name][name]<EOL>if not dirs:<EOL><INDENT>break<EOL><DEDENT>try:<EOL><INDENT>source, path = loader.load_template_source(name, dirs)<EOL><DEDENT>except TemplateDoesNotExist:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if not peeking:<EOL><INDENT>remove_path = os.path.abspath(path[:-len(name) - <NUM_LIT:1>])<EOL>context[context_name][name].remove(remove_path)<EOL><DEDENT>return Template(source)<EOL><DEDENT><DEDENT>raise TemplateDoesNotExist(name)<EOL>
Replacement for Django's ``find_template`` that uses the current
template context to keep track of which template directories it
has used when finding a template. This allows multiple templates
with the same relative name/path to be discovered, so that
circular template inheritance can occur.