Skip to main content

Loader

This class provides a caching mechanism for template loading to improve performance by storing both successfully loaded templates and missing template exceptions. It wraps multiple underlying loaders and manages a template cache that accounts for template debugging states to prevent memory leaks. The class also includes functionality to generate unique cache keys and reset the internal cache when necessary.

Attributes

AttributeTypeDescription
get_template_cachedict = {}A dictionary mapping generated cache keys to either successfully loaded Template objects or TemplateDoesNotExist exceptions to optimize subsequent lookups.
loaderslistA list of template loader instances initialized by the engine that this loader iterates through to find templates.

Constructor

Signature

def Loader(
engine: Any,
loaders: list
) - > null

Parameters

NameTypeDescription
engineAnyThe template engine instance used to retrieve loaders and initialize the base class.
loaderslistA collection of loader configurations or instances to be processed by the engine.

Methods


get_dirs()

@classmethod
def get_dirs() - > generator

Yields the directory paths from all sub-loaders that support directory listing.

Returns

TypeDescription
generatorAn iterable of directory paths used for template discovery

get_contents()

@classmethod
def get_contents(
origin: [Origin](../../base/origin.md?sid=django_template_base_origin)
) - > string

Retrieves the raw contents of a template based on its origin by delegating to the origin's associated loader.

Parameters

NameTypeDescription
origin[Origin](../../base/origin.md?sid=django_template_base_origin)The Origin object containing metadata about where the template is located

Returns

TypeDescription
stringThe source code or content of the template

get_template()

@classmethod
def get_template(
template_name: string,
skip: list = None
) - > [Template](../../base/template.md?sid=django_template_base_template)

Perform the caching that gives this loader its name. Often many of the templates attempted will be missing, so memory use is of concern here. To keep it in check, caching behavior is a little complicated when a template is not found. See ticket #26306 for more details. With template debugging disabled, cache the TemplateDoesNotExist class for every missing template and raise a new instance of it after fetching it from the cache. With template debugging enabled, a unique TemplateDoesNotExist object is cached for each missing template to preserve debug data. When raising an exception, Python sets traceback, context, and cause attributes on it. Those attributes can contain references to all sorts of objects up the call chain and caching them creates a memory leak. Thus, unraised copies of the exceptions are cached and copies of those copies are raised after they're fetched from the cache.

Parameters

NameTypeDescription
template_namestringThe path or identifier of the template to load
skiplist = NoneA list of Origin objects to exclude from the search to prevent circular inheritance

Returns

TypeDescription
[Template](../../base/template.md?sid=django_template_base_template)The compiled Template object retrieved from cache or loaded from the filesystem

get_template_sources()

@classmethod
def get_template_sources(
template_name: string
) - > generator

Iterates through all configured sub-loaders to yield potential Origin objects for a given template name.

Parameters

NameTypeDescription
template_namestringThe name of the template to locate across all sub-loaders

Returns

TypeDescription
generatorAn iterable of Origin objects representing possible locations for the template

cache_key()

@classmethod
def cache_key(
template_name: string,
skip: list = None
) - > string

Generate a cache key for the template name and skip. If skip is provided, only origins that match template_name are included in the cache key. This ensures each template is only parsed and cached once if contained in different extend chains.

Parameters

NameTypeDescription
template_namestringThe name of the template being cached
skiplist = NoneA list of Origin objects used to differentiate cache keys during complex template inheritance

Returns

TypeDescription
stringA unique string key used to store and retrieve templates from the internal cache

generate_hash()

@classmethod
def generate_hash(
values: list
) - > string

Creates a SHA1 hex digest from a list of strings to provide a unique identifier for cache key components.

Parameters

NameTypeDescription
valueslistA list of strings, typically origin names, to be hashed together

Returns

TypeDescription
stringA hexadecimal string representing the hash of the input values

reset()

@classmethod
def reset()

Empty the template cache.