Components¤
These are the components provided by django_components.
DynamicComponent
¤
Bases: django_components.component.Component
This component is given a registered name or a reference to another component, and behaves as if the other component was in its place.
The args, kwargs, and slot fills are all passed down to the underlying component.
Parameters:
-
is
(str | Type[Component]
) –Component that should be rendered. Either a registered name of a component, or a Component class directly. Required.
-
registry
(ComponentRegistry
, default:None
) –Specify the registry to search for the registered name. If omitted, all registries are searched until the first match.
-
*args
–Additional data passed to the component.
-
**kwargs
–Additional data passed to the component.
Slots:
- Any slots, depending on the actual component.
Examples:
Django
{% component "dynamic" is=table_comp data=table_data headers=table_headers %}
{% fill "pagination" %}
{% component "pagination" / %}
{% endfill %}
{% endcomponent %}
Or in case you use the django_components.component_shorthand_formatter
tag formatter:
{% dynamic is=table_comp data=table_data headers=table_headers %}
{% fill "pagination" %}
{% component "pagination" / %}
{% endfill %}
{% enddynamic %}
Python
from django_components import DynamicComponent
DynamicComponent.render(
kwargs={
"is": table_comp,
"data": table_data,
"headers": table_headers,
},
slots={
"pagination": PaginationComponent.render(
render_dependencies=False,
),
},
)
Use cases¤
Dynamic components are suitable if you are writing something like a form component. You may design it such that users give you a list of input types, and you render components depending on the input types.
While you could handle this with a series of if / else statements, that's not an extensible approach. Instead, you can use the dynamic component in place of normal components.
Component name¤
By default, the dynamic component is registered under the name "dynamic"
. In case of a conflict, you can set the COMPONENTS.dynamic_component_name
setting to change the name used for the dynamic components.
After which you will be able to use the dynamic component with the new name:
{% component "my_dynamic" is=table_comp data=table_data headers=table_headers %}
{% fill "pagination" %}
{% component "pagination" / %}
{% endfill %}
{% endcomponent %}