Skip to main content

Loader

This class provides a mechanism for loading template files from the file system based on a list of specified directories. It handles the retrieval of template source origins while ensuring security by excluding paths that fall outside the designated template directories. The class also manages reading file contents using the engine's defined character set.

Attributes

AttributeTypeDescription
dirslist of strings = NoneA list of directory paths where the loader will search for template files.

Constructor

Signature

def Loader(
engine: Any,
dirs: list[str]| None = None
) - > null

Parameters

NameTypeDescription
engineAnyThe template engine instance associated with this loader.
dirs`list[str]None` = None

Signature

def Loader(
engine: [Engine](../../engine/engine.md?sid=django_template_engine_engine),
dirs: list = None
) - > null

Parameters

NameTypeDescription
engine[Engine](../../engine/engine.md?sid=django_template_engine_engine)The template engine instance associated with this loader
dirslist = NoneAn optional list of directory paths to search for templates

Methods


get_dirs()

@classmethod
def get_dirs() - > list

Retrieves the list of directories to search, defaulting to the engine's configured directories if none were provided at initialization.

Returns

TypeDescription
listA list of directory paths used for template lookups

get_contents()

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

Reads and returns the raw content of a template file from the filesystem using the engine's configured charset.

Parameters

NameTypeDescription
origin[Origin](../../base/origin.md?sid=django_template_base_origin)An Origin object containing the absolute path to the template file

Returns

TypeDescription
stringThe string content of the template file

get_template_sources()

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

Return an Origin object pointing to an absolute path in each directory in template_dirs. For security reasons, if a path doesn't lie inside one of the template_dirs it is excluded from the result set.

Parameters

NameTypeDescription
template_namestringThe relative path or name of the template to locate

Returns

TypeDescription
generatorA generator yielding Origin objects for each valid directory match