component_registry ¤
Classes:
-
AlreadyRegistered–Raised when you try to register a Component,
-
ComponentRegistry–Manages components and makes them available
-
NotRegistered–Raised when you try to access a Component,
-
RegistrySettings–Configuration for a
ComponentRegistry.
Functions:
Attributes:
-
registry(ComponentRegistry) –The default and global component registry.
registry module-attribute ¤
registry: ComponentRegistry = ComponentRegistry()
The default and global component registry. Use this instance to directly register or remove components:
AlreadyRegistered ¤
Bases: Exception
Raised when you try to register a Component, but it's already registered with given ComponentRegistry.
ComponentRegistry ¤
ComponentRegistry(
library: Optional[Library] = None, settings: Optional[Union[RegistrySettings, Callable[[ComponentRegistry], RegistrySettings]]] = None
)
Manages components and makes them available in the template, by default as {% component %} tags.
To enable a component to be used in a template, the component must be registered with a component registry.
When you register a component to a registry, behind the scenes the registry automatically adds the component's template tag (e.g. {% component %} to the Library. And the opposite happens when you unregister a component - the tag is removed.
Parameters:
-
library(Library, default:None) –Django
Libraryassociated with this registry. If omitted, the default Library instance from django_components is used. -
settings(Union[RegistrySettings, Callable[[ComponentRegistry], RegistrySettings]], default:None) –Configure how the components registered with this registry will behave when rendered. See
RegistrySettings. Can be either a static value or a callable that returns the settings. If omitted, the settings fromCOMPONENTSare used.
Notes:
- The default registry is available as
django_components.registry. - The default registry is used when registering components with
@registerdecorator.
Example:
# Use with default Library
registry = ComponentRegistry()
# Or a custom one
my_lib = Library()
registry = ComponentRegistry(library=my_lib)
# Usage
registry.register("button", ButtonComponent)
registry.register("card", CardComponent)
registry.all()
registry.clear()
registry.get()
Using registry to share components¤
You can use component registry for isolating or "packaging" components:
-
Create new instance of
ComponentRegistryand Library: -
Register components to the registry:
-
In your target project, load the Library associated with the registry:
-
Use the registered components in your templates:
Methods:
-
all–Retrieve all registered
Componentclasses. -
clear–Clears the registry, unregistering all components.
-
get–Retrieve a
Component -
register–Register a
Componentclass -
unregister–Unregister the
Componentclass
Attributes:
-
library(Library) –The template tag
Library -
settings(InternalRegistrySettings) –Registry settings configured for this registry.
Source code in src/django_components/component_registry.py
settings property ¤
Registry settings configured for this registry.
all ¤
Retrieve all registered Component classes.
Returns:
-
Dict[str, Type[Component]]–Dict[str, Type[Component]]: A dictionary of component names to component classes
Example:
# First register components
registry.register("button", ButtonComponent)
registry.register("card", CardComponent)
# Then get all
registry.all()
# > {
# > "button": ButtonComponent,
# > "card": CardComponent,
# > }
Source code in src/django_components/component_registry.py
clear ¤
Clears the registry, unregistering all components.
Example:
# First register components
registry.register("button", ButtonComponent)
registry.register("card", CardComponent)
# Then clear
registry.clear()
# Then get all
registry.all()
# > {}
Source code in src/django_components/component_registry.py
get ¤
Retrieve a Component class registered under the given name.
Parameters:
-
name(str) –The name under which the component was registered. Required.
Returns:
Raises:
NotRegisteredif the given name is not registered.
Example:
# First register component
registry.register("button", ButtonComponent)
# Then get
registry.get("button")
# > ButtonComponent
Source code in src/django_components/component_registry.py
register ¤
Register a Component class with this registry under the given name.
A component MUST be registered before it can be used in a template such as:
Parameters:
-
name(str) –The name under which the component will be registered. Required.
-
component(Type[Component]) –The component class to register. Required.
Raises:
AlreadyRegisteredif a different component was already registered under the same name.
Example:
Source code in src/django_components/component_registry.py
unregister ¤
unregister(name: str) -> None
Unregister the Component class that was registered under the given name.
Once a component is unregistered, it is no longer available in the templates.
Parameters:
-
name(str) –The name under which the component is registered. Required.
Raises:
NotRegisteredif the given name is not registered.
Example:
# First register component
registry.register("button", ButtonComponent)
# Then unregister
registry.unregister("button")
Source code in src/django_components/component_registry.py
NotRegistered ¤
Bases: Exception
Raised when you try to access a Component, but it's NOT registered with given ComponentRegistry.
RegistrySettings ¤
Bases: NamedTuple
Configuration for a ComponentRegistry.
These settings define how the components registered with this registry will behave when rendered.
from django_components import ComponentRegistry, RegistrySettings
registry_settings = RegistrySettings(
context_behavior="django",
tag_formatter="django_components.component_shorthand_formatter",
)
registry = ComponentRegistry(settings=registry_settings)
Attributes:
-
CONTEXT_BEHAVIOR(Optional[ContextBehaviorType]) –Deprecated. Use
context_behaviorinstead. Will be removed in v1. -
TAG_FORMATTER(Optional[Union[TagFormatterABC, str]]) –Deprecated. Use
tag_formatterinstead. Will be removed in v1. -
context_behavior(Optional[ContextBehaviorType]) –Same as the global
-
tag_formatter(Optional[Union[TagFormatterABC, str]]) –Same as the global
CONTEXT_BEHAVIOR class-attribute instance-attribute ¤
CONTEXT_BEHAVIOR: Optional[ContextBehaviorType] = None
Deprecated. Use context_behavior instead. Will be removed in v1.
Same as the global COMPONENTS.context_behavior setting, but for this registry.
If omitted, defaults to the global COMPONENTS.context_behavior setting.
TAG_FORMATTER class-attribute instance-attribute ¤
TAG_FORMATTER: Optional[Union[TagFormatterABC, str]] = None
Deprecated. Use tag_formatter instead. Will be removed in v1.
Same as the global COMPONENTS.tag_formatter setting, but for this registry.
If omitted, defaults to the global COMPONENTS.tag_formatter setting.
context_behavior class-attribute instance-attribute ¤
context_behavior: Optional[ContextBehaviorType] = None
Same as the global COMPONENTS.context_behavior setting, but for this registry.
If omitted, defaults to the global COMPONENTS.context_behavior setting.
tag_formatter class-attribute instance-attribute ¤
tag_formatter: Optional[Union[TagFormatterABC, str]] = None
Same as the global COMPONENTS.tag_formatter setting, but for this registry.
If omitted, defaults to the global COMPONENTS.tag_formatter setting.
register ¤
register(name: str, registry: Optional[ComponentRegistry] = None) -> Callable[
[Type[Component[ArgsType, KwargsType, SlotsType, DataType, JsDataType, CssDataType]]],
Type[Component[ArgsType, KwargsType, SlotsType, DataType, JsDataType, CssDataType]],
]
Class decorator for registering a component to a component registry.
Parameters:
-
name(str) –Registered name. This is the name by which the component will be accessed from within a template when using the
{% component %}tag. Required. -
registry(ComponentRegistry, default:None) –Specify the registry to which to register this component. If omitted, component is registered to the default registry.
Raises:
-
AlreadyRegistered–If there is already a component registered under the same name.
Examples:
from django_components import Component, register
@register("my_component")
class MyComponent(Component):
...
Specifing ComponentRegistry the component should be registered to by setting the registry kwarg:
from django.template import Library
from django_components import Component, ComponentRegistry, register
my_lib = Library()
my_reg = ComponentRegistry(library=my_lib)
@register("my_component", registry=my_reg)
class MyComponent(Component):
...