Skip to content

Extension hooks¤

Overview of all the extension hooks available in Django Components.

Read more on Extensions.

Hooks¤

on_component_class_created ¤

on_component_class_created(ctx: OnComponentClassCreatedContext) -> None

See source code

Called when a new Component class is created.

This hook is called after the Component class is fully defined but before it's registered.

Use this hook to perform any initialization or validation of the Component class.

Example:

from django_components import ComponentExtension, OnComponentClassCreatedContext

class MyExtension(ComponentExtension):
    def on_component_class_created(self, ctx: OnComponentClassCreatedContext) -> None:
        # Add a new attribute to the Component class
        ctx.component_cls.my_attr = "my_value"

Available data:

name type description
component_cls Type[Component] The created Component class

on_component_class_deleted ¤

on_component_class_deleted(ctx: OnComponentClassDeletedContext) -> None

See source code

Called when a Component class is being deleted.

This hook is called before the Component class is deleted from memory.

Use this hook to perform any cleanup related to the Component class.

Example:

from django_components import ComponentExtension, OnComponentClassDeletedContext

class MyExtension(ComponentExtension):
    def on_component_class_deleted(self, ctx: OnComponentClassDeletedContext) -> None:
        # Remove Component class from the extension's cache on deletion
        self.cache.pop(ctx.component_cls, None)

Available data:

name type description
component_cls Type[Component] The to-be-deleted Component class

on_component_data ¤

on_component_data(ctx: OnComponentDataContext) -> None

See source code

Called when a Component was triggered to render, after a component's context and data methods have been processed.

This hook is called after Component.get_context_data(), Component.get_js_data() and Component.get_css_data().

This hook runs after on_component_input.

Use this hook to modify or validate the component's data before rendering.

Example:

from django_components import ComponentExtension, OnComponentDataContext

class MyExtension(ComponentExtension):
    def on_component_data(self, ctx: OnComponentDataContext) -> None:
        # Add extra template variable to all components when they are rendered
        ctx.context_data["my_template_var"] = "my_value"

Available data:

name type description
component Component The Component instance that is being rendered
component_cls Type[Component] The Component class
component_id str The unique identifier for this component instance
context_data Dict Dictionary of context data from Component.get_context_data()
css_data Dict Dictionary of CSS data from Component.get_css_data()
js_data Dict Dictionary of JavaScript data from Component.get_js_data()

on_component_input ¤

on_component_input(ctx: OnComponentInputContext) -> Optional[str]

See source code

Called when a Component was triggered to render, but before a component's context and data methods are invoked.

Use this hook to modify or validate component inputs before they're processed.

This is the first hook that is called when rendering a component. As such this hook is called before Component.get_context_data(), Component.get_js_data(), and Component.get_css_data() methods, and the on_component_data hook.

This hook also allows to skip the rendering of a component altogether. If the hook returns a non-null value, this value will be used instead of rendering the component.

You can use this to implement a caching mechanism for components, or define components that will be rendered conditionally.

Example:

from django_components import ComponentExtension, OnComponentInputContext

class MyExtension(ComponentExtension):
    def on_component_input(self, ctx: OnComponentInputContext) -> None:
        # Add extra kwarg to all components when they are rendered
        ctx.kwargs["my_input"] = "my_value"

Available data:

name type description
args List List of positional arguments passed to the component
component Component The Component instance that received the input and is being rendered
component_cls Type[Component] The Component class
component_id str The unique identifier for this component instance
context Context The Django template Context object
kwargs Dict Dictionary of keyword arguments passed to the component
slots Dict Dictionary of slot definitions

on_component_registered ¤

on_component_registered(ctx: OnComponentRegisteredContext) -> None

See source code

Called when a Component class is registered with a ComponentRegistry.

This hook is called after a Component class is successfully registered.

Example:

from django_components import ComponentExtension, OnComponentRegisteredContext

class MyExtension(ComponentExtension):
    def on_component_registered(self, ctx: OnComponentRegisteredContext) -> None:
        print(f"Component {ctx.component_cls} registered to {ctx.registry} as '{ctx.name}'")

Available data:

name type description
component_cls Type[Component] The registered Component class
name str The name the component was registered under
registry ComponentRegistry The registry the component was registered to

on_component_rendered ¤

on_component_rendered(ctx: OnComponentRenderedContext) -> Optional[str]

See source code

Called when a Component was rendered, including all its child components.

Use this hook to access or post-process the component's rendered output.

To modify the output, return a new string from this hook.

Example:

from django_components import ComponentExtension, OnComponentRenderedContext

class MyExtension(ComponentExtension):
    def on_component_rendered(self, ctx: OnComponentRenderedContext) -> Optional[str]:
        # Append a comment to the component's rendered output
        return ctx.result + "<!-- MyExtension comment -->"

Available data:

name type description
component Component The Component instance that is being rendered
component_cls Type[Component] The Component class
component_id str The unique identifier for this component instance
result str The rendered component

on_component_unregistered ¤

on_component_unregistered(ctx: OnComponentUnregisteredContext) -> None

See source code

Called when a Component class is unregistered from a ComponentRegistry.

This hook is called after a Component class is removed from the registry.

Example:

from django_components import ComponentExtension, OnComponentUnregisteredContext

class MyExtension(ComponentExtension):
    def on_component_unregistered(self, ctx: OnComponentUnregisteredContext) -> None:
        print(f"Component {ctx.component_cls} unregistered from {ctx.registry} as '{ctx.name}'")

Available data:

name type description
component_cls Type[Component] The unregistered Component class
name str The name the component was registered under
registry ComponentRegistry The registry the component was unregistered from

on_registry_created ¤

on_registry_created(ctx: OnRegistryCreatedContext) -> None

See source code

Called when a new ComponentRegistry is created.

This hook is called after a new ComponentRegistry instance is initialized.

Use this hook to perform any initialization needed for the registry.

Example:

from django_components import ComponentExtension, OnRegistryCreatedContext

class MyExtension(ComponentExtension):
    def on_registry_created(self, ctx: OnRegistryCreatedContext) -> None:
        # Add a new attribute to the registry
        ctx.registry.my_attr = "my_value"

Available data:

name type description
registry ComponentRegistry The created ComponentRegistry instance

on_registry_deleted ¤

on_registry_deleted(ctx: OnRegistryDeletedContext) -> None

See source code

Called when a ComponentRegistry is being deleted.

This hook is called before a ComponentRegistry instance is deleted.

Use this hook to perform any cleanup related to the registry.

Example:

from django_components import ComponentExtension, OnRegistryDeletedContext

class MyExtension(ComponentExtension):
    def on_registry_deleted(self, ctx: OnRegistryDeletedContext) -> None:
        # Remove registry from the extension's cache on deletion
        self.cache.pop(ctx.registry, None)

Available data:

name type description
registry ComponentRegistry The to-be-deleted ComponentRegistry instance

Objects¤

OnComponentClassCreatedContext ¤

Attributes:

component_cls instance-attribute ¤

component_cls: Type[Component]

See source code

The created Component class

OnComponentClassDeletedContext ¤

Attributes:

component_cls instance-attribute ¤

component_cls: Type[Component]

See source code

The to-be-deleted Component class

OnComponentDataContext ¤

Attributes:

component instance-attribute ¤

component: Component

See source code

The Component instance that is being rendered

component_cls instance-attribute ¤

component_cls: Type[Component]

See source code

The Component class

component_id instance-attribute ¤

component_id: str

See source code

The unique identifier for this component instance

context_data instance-attribute ¤

context_data: Dict

See source code

Dictionary of context data from Component.get_context_data()

css_data instance-attribute ¤

css_data: Dict

See source code

Dictionary of CSS data from Component.get_css_data()

js_data instance-attribute ¤

js_data: Dict

See source code

Dictionary of JavaScript data from Component.get_js_data()

OnComponentInputContext ¤

Attributes:

args instance-attribute ¤

args: List

See source code

List of positional arguments passed to the component

component instance-attribute ¤

component: Component

See source code

The Component instance that received the input and is being rendered

component_cls instance-attribute ¤

component_cls: Type[Component]

See source code

The Component class

component_id instance-attribute ¤

component_id: str

See source code

The unique identifier for this component instance

context instance-attribute ¤

context: Context

See source code

The Django template Context object

kwargs instance-attribute ¤

kwargs: Dict

See source code

Dictionary of keyword arguments passed to the component

slots instance-attribute ¤

slots: Dict

See source code

Dictionary of slot definitions

OnComponentRegisteredContext ¤

Attributes:

component_cls instance-attribute ¤

component_cls: Type[Component]

See source code

The registered Component class

name instance-attribute ¤

name: str

See source code

The name the component was registered under

registry instance-attribute ¤

See source code

The registry the component was registered to

OnComponentUnregisteredContext ¤

Attributes:

component_cls instance-attribute ¤

component_cls: Type[Component]

See source code

The unregistered Component class

name instance-attribute ¤

name: str

See source code

The name the component was registered under

registry instance-attribute ¤

See source code

The registry the component was unregistered from

OnRegistryCreatedContext ¤

Attributes:

registry instance-attribute ¤

See source code

The created ComponentRegistry instance

OnRegistryDeletedContext ¤

Attributes:

registry instance-attribute ¤

See source code

The to-be-deleted ComponentRegistry instance