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
| Attribute | Type | Description |
|---|
| get_template_cache | dict = {} | A dictionary mapping generated cache keys to either successfully loaded Template objects or TemplateDoesNotExist exceptions to optimize subsequent lookups. |
| loaders | list | A 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
| Name | Type | Description |
|---|
| engine | Any | The template engine instance used to retrieve loaders and initialize the base class. |
| loaders | list | A 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
| Type | Description |
|---|
generator | An 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
| Name | Type | Description |
|---|
| origin | [Origin](../../base/origin.md?sid=django_template_base_origin) | The Origin object containing metadata about where the template is located |
Returns
| Type | Description |
|---|
string | The 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
| Name | Type | Description |
|---|
| template_name | string | The path or identifier of the template to load |
| skip | list = None | A list of Origin objects to exclude from the search to prevent circular inheritance |
Returns
| Type | Description |
|---|
[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
| Name | Type | Description |
|---|
| template_name | string | The name of the template to locate across all sub-loaders |
Returns
| Type | Description |
|---|
generator | An 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
| Name | Type | Description |
|---|
| template_name | string | The name of the template being cached |
| skip | list = None | A list of Origin objects used to differentiate cache keys during complex template inheritance |
Returns
| Type | Description |
|---|
string | A 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
| Name | Type | Description |
|---|
| values | list | A list of strings, typically origin names, to be hashed together |
Returns
| Type | Description |
|---|
string | A hexadecimal string representing the hash of the input values |
reset()
Empty the template cache.