Skip to main content

Engine

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

AttributeTypeDescription
default_builtinslist = ['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.
dirslist = []List of filesystem directories where the engine should look for template source files in order.
app_dirsboolean = falseBoolean flag indicating whether the engine should look for templates inside installed application directories.
autoescapeboolean = trueDetermines whether HTML auto-escaping is enabled by default for all rendered template output.
context_processorslist = []List of dotted Python paths to callables used to populate the context when rendering a template.
debugboolean = falseBoolean setting that turns on template debug mode, providing detailed error reports for template syntax or rendering errors.
loaderslist = nullList of template loader classes or configurations used to locate and read template source files.
string_if_invalidstring = ""The string output used by the template system when a variable is invalid or missing.
file_charsetstring = "utf-8"The character encoding used to read template files from disk.
librariesdict = {}Dictionary mapping library names to dotted Python paths for custom template tags and filters.
template_librariesdict = nullDictionary of loaded template library objects mapped by their registered names.
builtinslist = nullThe complete list of dotted Python paths for template modules that are automatically available in every template.
template_builtinslist = nullList 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

NameTypeDescription
dirslist = NoneDirectories where the engine looks for template source files, in order.
app_dirsbool = FalseWhether the engine should look for templates inside installed applications.
context_processorslist = NoneA list of dotted Python paths to callables that are used to populate the context.
debugbool = FalseWhether to enable template debug mode.
loaderslist = NoneA list of template loader classes, specified as dotted paths.
string_if_invalidstr = ""The string the template system should use for invalid variables.
file_charsetstr = "utf-8"The charset used to read template files on disk.
librariesdict = NoneA dictionary of template tag modules to register with the engine.
builtinslist = NoneA list of dotted Python paths to template tag modules to add to built-ins.
autoescapebool = TrueWhether 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

TypeDescription
[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

TypeDescription
tupleA 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

NameTypeDescription
builtinslistA list of dotted Python paths to template tag/filter modules.

Returns

TypeDescription
listA 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

NameTypeDescription
librariesdictA dictionary where keys are library names and values are dotted Python paths.

Returns

TypeDescription
dictA 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

TypeDescription
listA 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

NameTypeDescription
template_loaderslistA list of loader class paths or configuration tuples.

Returns

TypeDescription
listA 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

NameTypeDescription
loader`stringtuple

Returns

TypeDescription
objectAn 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

NameTypeDescription
namestringThe name of the template to locate.
dirslist = nullOptional list of directories to search in addition to the default search path.
skiplist = nullA list of origins to skip when searching, used to avoid recursion in template inheritance.

Returns

TypeDescription
tupleA 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

NameTypeDescription
template_codestringThe raw template source code to be compiled.

Returns

TypeDescription
[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

NameTypeDescription
template_namestringThe name of the template file, optionally including a '#' fragment for partials.

Returns

TypeDescription
[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

NameTypeDescription
template_name`stringlist
context`dictContext` = null

Returns

TypeDescription
stringThe 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

NameTypeDescription
template_name_listlistA sequence of template names to search for in order of preference.

Returns

TypeDescription
[Template](../base/template.md?sid=django_template_base_template)The first compiled Template object found in the provided list.