[docs]classEngine:default_builtins=['django.template.defaulttags','django.template.defaultfilters','django.template.loader_tags',]def__init__(self,dirs=None,app_dirs=False,context_processors=None,debug=False,loaders=None,string_if_invalid='',file_charset='utf-8',libraries=None,builtins=None,autoescape=True):ifdirsisNone:dirs=[]ifcontext_processorsisNone:context_processors=[]ifloadersisNone:loaders=['django.template.loaders.filesystem.Loader']ifapp_dirs:loaders+=['django.template.loaders.app_directories.Loader']ifnotdebug:loaders=[('django.template.loaders.cached.Loader',loaders)]else:ifapp_dirs:raiseImproperlyConfigured("app_dirs must not be set when loaders is defined.")iflibrariesisNone:libraries={}ifbuiltinsisNone:builtins=[]self.dirs=dirsself.app_dirs=app_dirsself.autoescape=autoescapeself.context_processors=context_processorsself.debug=debugself.loaders=loadersself.string_if_invalid=string_if_invalidself.file_charset=file_charsetself.libraries=librariesself.template_libraries=self.get_template_libraries(libraries)self.builtins=self.default_builtins+builtinsself.template_builtins=self.get_template_builtins(self.builtins)

[docs]@staticmethod@functools.lru_cache()defget_default():""" Return the first DjangoTemplates backend that's configured, or raise ImproperlyConfigured if none are configured. This is required for preserving historical APIs that rely on a globally available, implicitly configured engine such as: >>> from django.template import Context, Template >>> template = Template("Hello {{ name }}!") >>> context = Context({'name': "world"}) >>> template.render(context) 'Hello world!' """# Since Engine is imported in django.template and since# DjangoTemplates is a wrapper around this Engine class,# local imports are required to avoid import loops.fromdjango.templateimportenginesfromdjango.template.backends.djangoimportDjangoTemplatesforengineinengines.all():ifisinstance(engine,DjangoTemplates):returnengine.engineraiseImproperlyConfigured('No DjangoTemplates backend is configured.')

defrender_to_string(self,template_name,context=None):""" Render the template specified by template_name with the given context. For use in Django's test suite. """ifisinstance(template_name,(list,tuple)):t=self.select_template(template_name)else:t=self.get_template(template_name)# Django < 1.8 accepted a Context in `context` even though that's# unintended. Preserve this ability but don't rewrap `context`.ifisinstance(context,Context):returnt.render(context)else:returnt.render(Context(context))

[docs]defselect_template(self,template_name_list):""" Given a list of template names, return the first that can be loaded. """ifnottemplate_name_list:raiseTemplateDoesNotExist("No template names provided")not_found=[]fortemplate_nameintemplate_name_list:try:returnself.get_template(template_name)exceptTemplateDoesNotExistasexc:ifexc.args[0]notinnot_found:not_found.append(exc.args[0])continue# If we get here, none of the templates could be loadedraiseTemplateDoesNotExist(', '.join(not_found))