template - Django-Components" > template - Django-Components" >
Skip to content

template ¤

cached_template ¤

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 TEMPLATE_CACHE_SIZE setting.

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 `TEMPLATE_CACHE_SIZE` setting."""
    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