Jinja2
This class provides a template engine backend for Jinja2, enabling the loading and rendering of templates within the application framework. It initializes a Jinja2 environment with configurable options such as loaders, context processors, and auto-escaping, while mapping native Jinja2 exceptions to framework-specific template errors. The class supports creating templates from both strings and named files through its integration with the Jinja2 library.
Attributes
| Attribute | Type | Description |
|---|---|---|
| app_dirname | string = "jinja2" | The name of the subdirectory within installed applications where the engine looks for templates. |
Constructor
Signature
def Jinja2(
params: dict
)
Parameters
| Name | Type | Description |
|---|---|---|
| params | dict | A dictionary of configuration settings used to initialize the engine and its environment. |
Methods
from_string()
@classmethod
def from_string(
template_code: string
) - > [Template](../../base/template.md?sid=django_template_base_template)
Compiles a Jinja2 template from the provided raw string code. Use this to generate templates dynamically at runtime rather than loading them from the filesystem.
Parameters
| Name | Type | Description |
|---|---|---|
| template_code | string | The raw Jinja2 template source code to be compiled |
Returns
| Type | Description |
|---|---|
[Template](../../base/template.md?sid=django_template_base_template) | A Template object instance wrapping the compiled Jinja2 template code |
get_template()
@classmethod
def get_template(
template_name: string
) - > [Template](../../base/template.md?sid=django_template_base_template)
Loads and compiles a template file from the configured template directories. This method handles Jinja2 specific exceptions and re-raises them as Django-compatible TemplateDoesNotExist or TemplateSyntaxError exceptions.
Parameters
| Name | Type | Description |
|---|---|---|
| template_name | string | The path or identifier of the template file to load relative to the configured loaders |
Returns
| Type | Description |
|---|---|
[Template](../../base/template.md?sid=django_template_base_template) | A Template object instance corresponding to the requested template name |
template_context_processors()
@classmethod
def template_context_processors() - > list
Retrieves a list of callable context processors defined in the engine options. These processors are used to inject common variables into the template context during rendering.
Returns
| Type | Description |
|---|---|
list | A list of imported callable functions used to process template contexts |