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
| Attribute | Type | Description |
|---|---|---|
| dirs | list of strings = None | A 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
| Name | Type | Description |
|---|---|---|
| engine | Any | The 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
| Name | Type | Description |
|---|---|---|
| engine | [Engine](../../engine/engine.md?sid=django_template_engine_engine) | The template engine instance associated with this loader |
| dirs | list = None | An 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
| Type | Description |
|---|---|
list | A 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
| Name | Type | Description |
|---|---|---|
| origin | [Origin](../../base/origin.md?sid=django_template_base_origin) | An Origin object containing the absolute path to the template file |
Returns
| Type | Description |
|---|---|
string | The 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
| Name | Type | Description |
|---|---|---|
| template_name | string | The relative path or name of the template to locate |
Returns
| Type | Description |
|---|---|
generator | A generator yielding Origin objects for each valid directory match |