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
Available data:
name | type | description |
---|---|---|
component_cls | Type[Component] | The created Component class |
on_component_class_deleted ¤
on_component_class_deleted(ctx: OnComponentClassDeletedContext) -> None
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
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_template_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:
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 | Deprecated. Use template_data instead. Will be removed in v1.0. |
css_data | Dict | Dictionary of CSS data from Component.get_css_data() |
js_data | Dict | Dictionary of JavaScript data from Component.get_js_data() |
template_data | Dict | Dictionary of template data from Component.get_template_data() |
on_component_input ¤
on_component_input(ctx: OnComponentInputContext) -> Optional[str]
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_template_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"
Warning
In this hook, the components' inputs are still mutable.
As such, if a component defines Args
, Kwargs
, Slots
types, these types are NOT yet instantiated.
Instead, component fields like Component.args
, Component.kwargs
, Component.slots
are plain list
/ dict
objects.
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[str, Slot] | Dictionary of slot definitions |
on_component_registered ¤
on_component_registered(ctx: OnComponentRegisteredContext) -> None
Called when a Component
class is registered with a ComponentRegistry
.
This hook is called after a Component
class is successfully registered.
Example:
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 ¤
Called when a Component
was rendered, including all its child components.
Use this hook to access or post-process the component's rendered output.
This hook works similarly to Component.on_render_after()
:
-
To modify the output, return a new string from this hook. The original output or error will be ignored.
-
To cause this component to return a new error, raise that error. The original output and error will be ignored.
-
If you neither raise nor return string, the original output or error will be used.
Examples:
Change the final output of a component:
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 -->"
Cause the component to raise a new exception:
from django_components import ComponentExtension, OnComponentRenderedContext
class MyExtension(ComponentExtension):
def on_component_rendered(self, ctx: OnComponentRenderedContext) -> Optional[str]:
# Raise a new exception
raise Exception("Error message")
Return nothing (or None
) to handle the result as usual:
from django_components import ComponentExtension, OnComponentRenderedContext
class MyExtension(ComponentExtension):
def on_component_rendered(self, ctx: OnComponentRenderedContext) -> Optional[str]:
if ctx.error is not None:
# The component raised an exception
print(f"Error: {ctx.error}")
else:
# The component rendered successfully
print(f"Result: {ctx.result}")
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 |
error | Optional[Exception] | The error that occurred during rendering, or None if rendering was successful |
result | Optional[str] | The rendered component, or None if rendering failed |
on_component_unregistered ¤
on_component_unregistered(ctx: OnComponentUnregisteredContext) -> None
Called when a Component
class is unregistered from a ComponentRegistry
.
This hook is called after a Component
class is removed from the registry.
Example:
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_css_loaded ¤
Called when a Component's CSS is loaded as a string.
This hook runs only once per Component
class and works for both Component.css
and Component.css_file
.
Use this hook to read or modify the CSS.
To modify the CSS, return a new string from this hook.
Example:
Available data:
name | type | description |
---|---|---|
component_cls | Type[Component] | The Component class whose CSS was loaded |
content | str | The CSS content (string) |
on_js_loaded ¤
Called when a Component's JS is loaded as a string.
This hook runs only once per Component
class and works for both Component.js
and Component.js_file
.
Use this hook to read or modify the JS.
To modify the JS, return a new string from this hook.
Example:
Available data:
name | type | description |
---|---|---|
component_cls | Type[Component] | The Component class whose JS was loaded |
content | str | The JS content (string) |
on_registry_created ¤
on_registry_created(ctx: OnRegistryCreatedContext) -> None
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:
Available data:
name | type | description |
---|---|---|
registry | ComponentRegistry | The created ComponentRegistry instance |
on_registry_deleted ¤
on_registry_deleted(ctx: OnRegistryDeletedContext) -> None
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:
Available data:
name | type | description |
---|---|---|
registry | ComponentRegistry | The to-be-deleted ComponentRegistry instance |
on_slot_rendered ¤
Called when a {% slot %}
tag was rendered.
Use this hook to access or post-process the slot's rendered output.
To modify the output, return a new string from this hook.
Example:
from django_components import ComponentExtension, OnSlotRenderedContext
class MyExtension(ComponentExtension):
def on_slot_rendered(self, ctx: OnSlotRenderedContext) -> Optional[str]:
# Append a comment to the slot's rendered output
return ctx.result + "<!-- MyExtension comment -->"
Access slot metadata:
You can access the {% slot %}
tag node (SlotNode
) and its metadata using ctx.slot_node
.
For example, to find the Component
class to which belongs the template where the {% slot %}
tag is defined, you can use ctx.slot_node.template_component
:
from django_components import ComponentExtension, OnSlotRenderedContext
class MyExtension(ComponentExtension):
def on_slot_rendered(self, ctx: OnSlotRenderedContext) -> Optional[str]:
# Access slot metadata
slot_node = ctx.slot_node
slot_owner = slot_node.template_component
print(f"Slot owner: {slot_owner}")
Available data:
name | type | description |
---|---|---|
component | Component | The Component instance that contains the {% slot %} tag |
component_cls | Type[Component] | The Component class that contains the {% slot %} tag |
component_id | str | The unique identifier for this component instance |
result | SlotResult | The rendered result of the slot |
slot | Slot | The Slot instance that was rendered |
slot_is_default | bool | Whether the slot is default |
slot_is_required | bool | Whether the slot is required |
slot_name | str | The name of the {% slot %} tag |
slot_node | SlotNode | The node instance of the {% slot %} tag |
on_template_compiled ¤
Called when a Component's template is compiled into a Template
object.
This hook runs only once per Component
class and works for both Component.template
and Component.template_file
.
Use this hook to read or modify the template (in-place) after it's compiled.
Example:
Available data:
name | type | description |
---|---|---|
component_cls | Type[Component] | The Component class whose template was loaded |
template | django.template.base.Template | The compiled template object |
on_template_loaded ¤
Called when a Component's template is loaded as a string.
This hook runs only once per Component
class and works for both Component.template
and Component.template_file
.
Use this hook to read or modify the template before it's compiled.
To modify the template, return a new string from this hook.
Example:
Available data:
name | type | description |
---|---|---|
component_cls | Type[Component] | The Component class whose template was loaded |
content | str | The template string |
name | Optional[str] | The name of the template |
origin | Optional[django.template.base.Origin] | The origin of the template |
Objects¤
OnComponentClassCreatedContext ¤
Attributes:
-
component_cls
(Type[Component]
) –
component_cls instance-attribute
¤
The created Component class
OnComponentClassDeletedContext ¤
Attributes:
-
component_cls
(Type[Component]
) –
component_cls instance-attribute
¤
The to-be-deleted Component class
OnComponentDataContext ¤
Attributes:
-
component
(Component
) – -
component_cls
(Type[Component]
) – -
component_id
(str
) – -
context_data
(Dict
) – -
css_data
(Dict
) – -
js_data
(Dict
) – -
template_data
(Dict
) –
component_cls instance-attribute
¤
The Component class
component_id instance-attribute
¤
component_id: str
The unique identifier for this component instance
context_data instance-attribute
¤
context_data: Dict
Deprecated. Use template_data
instead. Will be removed in v1.0.
js_data instance-attribute
¤
js_data: Dict
Dictionary of JavaScript data from Component.get_js_data()
OnComponentInputContext ¤
Attributes:
-
args
(List
) – -
component
(Component
) – -
component_cls
(Type[Component]
) – -
component_id
(str
) – -
context
(Context
) – -
kwargs
(Dict
) – -
slots
(Dict[str, Slot]
) –
component instance-attribute
¤
component: Component
The Component instance that received the input and is being rendered
component_cls instance-attribute
¤
The Component class
component_id instance-attribute
¤
component_id: str
The unique identifier for this component instance
OnComponentRegisteredContext ¤
Attributes:
-
component_cls
(Type[Component]
) – -
name
(str
) – -
registry
(ComponentRegistry
) –
component_cls instance-attribute
¤
The registered Component class
registry instance-attribute
¤
registry: ComponentRegistry
The registry the component was registered to
OnComponentUnregisteredContext ¤
Attributes:
-
component_cls
(Type[Component]
) – -
name
(str
) – -
registry
(ComponentRegistry
) –
component_cls instance-attribute
¤
The unregistered Component class
registry instance-attribute
¤
registry: ComponentRegistry
The registry the component was unregistered from
OnRegistryCreatedContext ¤
Attributes:
-
registry
(ComponentRegistry
) –
OnRegistryDeletedContext ¤
Attributes:
-
registry
(ComponentRegistry
) –
registry instance-attribute
¤
registry: ComponentRegistry
The to-be-deleted ComponentRegistry instance