This class serves as the core template engine responsible for loading, compiling, and rendering templates. It manages configuration for template directories, loaders, context processors, and built-in tags or filters. The class provides high-level interfaces to retrieve templates from strings or files and supports advanced features like template inheritance and partial rendering.
Attributes
| Attribute | Type | Description |
|---|
| default_builtins | list = ['django.template.defaulttags', 'django.template.defaultfilters', 'django.template.loader_tags'] | List of dotted Python paths to the default template tag and filter modules loaded by the engine. |
| dirs | list = [] | List of filesystem directories where the engine should look for template source files in order. |
| app_dirs | boolean = false | Boolean flag indicating whether the engine should look for templates inside installed application directories. |
| autoescape | boolean = true | Determines whether HTML auto-escaping is enabled by default for all rendered template output. |
| context_processors | list = [] | List of dotted Python paths to callables used to populate the context when rendering a template. |
| debug | boolean = false | Boolean setting that turns on template debug mode, providing detailed error reports for template syntax or rendering errors. |
| loaders | list = null | List of template loader classes or configurations used to locate and read template source files. |
| string_if_invalid | string = "" | The string output used by the template system when a variable is invalid or missing. |
| file_charset | string = "utf-8" | The character encoding used to read template files from disk. |
| libraries | dict = {} | Dictionary mapping library names to dotted Python paths for custom template tags and filters. |
| template_libraries | dict = null | Dictionary of loaded template library objects mapped by their registered names. |
| builtins | list = null | The complete list of dotted Python paths for template modules that are automatically available in every template. |
| template_builtins | list = null | List of imported library objects that provide the built-in tags and filters for the engine. |
Constructor
Signature
def Engine(
dirs: list = None,
app_dirs: bool = False,
context_processors: list = None,
debug: bool = False,
loaders: list = None,
string_if_invalid: str = "",
file_charset: str = "utf-8",
libraries: dict = None,
builtins: list = None,
autoescape: bool = True
) - > null
Parameters
| Name | Type | Description |
|---|
| dirs | list = None | Directories where the engine looks for template source files, in order. |
| app_dirs | bool = False | Whether the engine should look for templates inside installed applications. |
| context_processors | list = None | A list of dotted Python paths to callables that are used to populate the context. |
| debug | bool = False | Whether to enable template debug mode. |
| loaders | list = None | A list of template loader classes, specified as dotted paths. |
| string_if_invalid | str = "" | The string the template system should use for invalid variables. |
| file_charset | str = "utf-8" | The charset used to read template files on disk. |
| libraries | dict = None | A dictionary of template tag modules to register with the engine. |
| builtins | list = None | A list of dotted Python paths to template tag modules to add to built-ins. |
| autoescape | bool = True | Whether to enable HTML auto-escaping of all variable outputs. |
Methods
get_default()
@classmethod
def get_default() - > [Engine](engine.md?sid=django_template_engine_engine)
Return the first DjangoTemplates backend that's configured, or raise ImproperlyConfigured if none are configured.
Returns
| Type | Description |
|---|
[Engine](engine.md?sid=django_template_engine_engine) | The underlying Engine instance of the first configured DjangoTemplates backend. |
template_context_processors()
@classmethod
def template_context_processors() - > tuple
Provides a tuple of callable context processors, combining built-in defaults with those specified in the engine configuration.
Returns
| Type | Description |
|---|
tuple | A tuple of imported context processor functions. |
get_template_builtins()
@classmethod
def get_template_builtins(
builtins: list
) - > list
Imports and returns a list of template library objects to be used as built-ins for every template.
Parameters
| Name | Type | Description |
|---|
| builtins | list | A list of dotted Python paths to template tag/filter modules. |
Returns
| Type | Description |
|---|
list | A list of imported template library instances. |
get_template_libraries()
@classmethod
def get_template_libraries(
libraries: dict
) - > dict
Maps library names to their imported library objects based on the provided configuration dictionary.
Parameters
| Name | Type | Description |
|---|
| libraries | dict | A dictionary where keys are library names and values are dotted Python paths. |
Returns
| Type | Description |
|---|
dict | A dictionary mapping library aliases to imported library instances. |
template_loaders()
@classmethod
def template_loaders() - > list
Retrieves the initialized template loader instances used by the engine to locate template files.
Returns
| Type | Description |
|---|
list | A list of instantiated template loader objects. |
get_template_loaders()
@classmethod
def get_template_loaders(
template_loaders: list
) - > list
Iterates through loader configurations and instantiates the corresponding loader classes.
Parameters
| Name | Type | Description |
|---|
| template_loaders | list | A list of loader class paths or configuration tuples. |
Returns
| Type | Description |
|---|
list | A list of successfully initialized template loader instances. |
find_template_loader()
@classmethod
def find_template_loader(
loader: string|tuple|list
) - > object
Instantiates a single template loader based on a string path or a configuration tuple.
Parameters
| Name | Type | Description |
|---|
| loader | `string | tuple |
Returns
| Type | Description |
|---|
object | An instance of the requested template loader class. |
find_template()
@classmethod
def find_template(
name: string,
dirs: list = null,
skip: list = null
) - > tuple
Iterates through configured loaders to find a template by name, returning the template content and its origin.
Parameters
| Name | Type | Description |
|---|
| name | string | The name of the template to locate. |
| dirs | list = null | Optional list of directories to search in addition to the default search path. |
| skip | list = null | A list of origins to skip when searching, used to avoid recursion in template inheritance. |
Returns
| Type | Description |
|---|
tuple | A tuple containing the template content and its Origin object. |
from_string()
@classmethod
def from_string(
template_code: string
) - > [Template](../base/template.md?sid=django_template_base_template)
Return a compiled Template object for the given template code, handling template inheritance recursively.
Parameters
| Name | Type | Description |
|---|
| template_code | string | The raw template source code to be compiled. |
Returns
| Type | Description |
|---|
[Template](../base/template.md?sid=django_template_base_template) | A compiled Template instance ready for rendering. |
get_template()
@classmethod
def get_template(
template_name: string
) - > [Template](../base/template.md?sid=django_template_base_template)
Return a compiled Template object for the given template name, handling template inheritance recursively.
Parameters
| Name | Type | Description |
|---|
| template_name | string | The name of the template file, optionally including a '#' fragment for partials. |
Returns
| Type | Description |
|---|
[Template](../base/template.md?sid=django_template_base_template) | A compiled Template instance or a specific partial if a fragment identifier was used. |
render_to_string()
@classmethod
def render_to_string(
template_name: string|list|tuple,
context: dict|Context = null
) - > string
Render the template specified by template_name with the given context. For use in Django's test suite.
Parameters
| Name | Type | Description |
|---|
| template_name | `string | list |
| context | `dict | Context` = null |
Returns
| Type | Description |
|---|
string | The fully rendered template content as a string. |
select_template()
@classmethod
def select_template(
template_name_list: list
) - > [Template](../base/template.md?sid=django_template_base_template)
Given a list of template names, return the first that can be loaded.
Parameters
| Name | Type | Description |
|---|
| template_name_list | list | A sequence of template names to search for in order of preference. |
Returns
| Type | Description |
|---|
[Template](../base/template.md?sid=django_template_base_template) | The first compiled Template object found in the provided list. |