Create a Template instance that will be cached as per the COMPONENTS.template_cache_size
setting.
Parameters:
-
template_string
(str
) β Template as a string, same as the first argument to Django's Template
. Required.
-
template_cls
(Type[Template]
, default: None
) β Specify the Template class that should be instantiated. Defaults to Django's Template
class.
-
origin
(Type[Origin]
, default: None
) β -
name
(Type[str]
, default: None
) β -
engine
(Type[Any]
, default: None
) β
from django_components import cached_template
template = cached_template("Variable: {{ variable }}")
# You can optionally specify Template class, and other Template inputs:
class MyTemplate(Template):
pass
template = cached_template(
"Variable: {{ variable }}",
template_cls=MyTemplate,
name=...
origin=...
engine=...
)
Source code in src/django_components/template.py
| def cached_template(
template_string: str,
template_cls: Optional[Type[Template]] = None,
origin: Optional[Origin] = None,
name: Optional[str] = None,
engine: Optional[Any] = None,
) -> Template:
"""
Create a Template instance that will be cached as per the
[`COMPONENTS.template_cache_size`](../settings#django_components.app_settings.ComponentsSettings.template_cache_size)
setting.
Args:
template_string (str): Template as a string, same as the first argument to Django's\
[`Template`](https://docs.djangoproject.com/en/5.1/topics/templates/#template). Required.
template_cls (Type[Template], optional): Specify the Template class that should be instantiated.\
Defaults to Django's [`Template`](https://docs.djangoproject.com/en/5.1/topics/templates/#template) class.
origin (Type[Origin], optional): Sets \
[`Template.Origin`](https://docs.djangoproject.com/en/5.1/howto/custom-template-backend/#origin-api-and-3rd-party-integration).
name (Type[str], optional): Sets `Template.name`
engine (Type[Any], optional): Sets `Template.engine`
```python
from django_components import cached_template
template = cached_template("Variable: {{ variable }}")
# You can optionally specify Template class, and other Template inputs:
class MyTemplate(Template):
pass
template = cached_template(
"Variable: {{ variable }}",
template_cls=MyTemplate,
name=...
origin=...
engine=...
)
```
""" # noqa: E501
template = _create_template(template_cls or Template, template_string, engine)
# Assign the origin and name separately, so the caching doesn't depend on them
# Since we might be accessing a template from cache, we want to define these only once
if not getattr(template, "_dc_cached", False):
template.origin = origin or Origin(UNKNOWN_SOURCE)
template.name = name
template._dc_cached = True
return template
|