You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/components/cohort.md
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,17 @@
1
1
---
2
2
title: Component - Cohort
3
3
order: 2
4
-
description: Cohort component in Unfold theme
4
+
description: A powerful visualization component for cohort analysis that displays user behavior patterns over time in a structured table format, perfect for tracking retention and engagement metrics in Django Unfold admin.
5
5
---
6
6
7
7
# Cohort component
8
8
9
-
The cohort component allows you to display cohort analysis data in a table format. It's useful for analyzing user behavior and retention over time. Unfold provides a default implementation of the cohort component. The only important parameter is the `data` parameter, which is a dictionary that contains a more complex data structure.
9
+
The cohort component is a powerful visualization tool that presents cohort analysis data in a structured table format. It enables you to track and analyze how groups of users (cohorts) behave over specific time periods, making it particularly valuable for measuring retention, engagement patterns, and user lifecycle metrics. Unfold provides a ready-to-use implementation of this component that requires minimal setup. To use it, you only need to provide the `data` parameter - a dictionary containing the cohort information with a specific structure that defines headers, rows, and cell values for the analysis table.
10
10
11
11
12
12
## Default cohort component implementation in template
13
13
14
-
In template you can use the default implementation of the cohort component by using the following code below. The `component_class` parameter is the name of the component class that you will create in the next section and it will prepare the data for the cohort component.
14
+
You can use the default implementation of the cohort component in your template with the following code. The `component_class` parameter refers to the name of the component class that will prepare the data for the cohort component, which we'll create in the next section.
15
15
16
16
```html
17
17
{% load unfold %}
@@ -20,7 +20,7 @@ In template you can use the default implementation of the cohort component by us
20
20
{% endcomponent %}
21
21
```
22
22
23
-
If you don't want to use `component_class` parameter, you can prepare `data` in the dashboard callback function and use it like this:
23
+
Alternatively, if you prefer not to use the `component_class` parameter, you can prepare the `data`directly in your dashboard callback function and pass it to the component like this:
24
24
25
25
```html
26
26
{% load unfold %}
@@ -31,13 +31,14 @@ If you don't want to use `component_class` parameter, you can prepare `data` in
31
31
32
32
## Custom cohort data preparation in component class
33
33
34
-
Below you can see an example of a component class that prepares the data for the cohort component. The component in templatewill receive the `data` parameter that is passed to the `get_context_data` method.
34
+
Here's an example of a component class that prepares data for the cohort component. When used in a template, the component will receive the `data` parameter that has been processed and returned by the `get_context_data` method.
35
35
36
36
```python
37
37
# admin.py
38
38
39
-
from unfold.components import BaseComponent
39
+
from unfold.components import BaseComponent, register_component
Copy file name to clipboardExpand all lines: docs/components/component_class.md
+16-4Lines changed: 16 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,26 @@
1
1
---
2
2
title: Component class
3
3
order: 1
4
-
description: Component class for custom component preprocessing
4
+
description: Create and register custom component classes in Django Unfold to preprocess data before rendering, separating business logic from presentation templates for cleaner, more maintainable code.
5
5
---
6
6
7
7
# Component class
8
8
9
-
Each component in Unfold theme has a component class that is responsible for preparing the data for the component. The component class is optional and if you don't want to use it, you can pass the data directly to the component in the template.
9
+
Unfold's component system features specialized component classes designed to preprocess and prepare data for rendering. These component classes are completely optional - you have the flexibility to either leverage them for handling data preparation logic or bypass them entirely by passing data directly to components in your templates. The choice depends on your specific needs and preferences for each use case.
10
+
11
+
## Component registration
12
+
13
+
For Unfold to recognize and utilize your custom component classes, each class must be registered within the system using the `register_component` decorator. This registration process is essential as it makes your component discoverable by Unfold's component rendering system, allowing it to be referenced by name in templates.
14
+
15
+
The registration mechanism creates a mapping between the component class name and its implementation, enabling the template engine to instantiate the correct class when the component is invoked. Without proper registration, Unfold would be unable to locate your component class when referenced in template files.
10
16
11
17
```python
12
18
# admin.py
13
19
14
-
from unfold.components import BaseComponent
20
+
from unfold.components import BaseComponent, register_component
15
21
22
+
23
+
@register_component
16
24
classMyComponent(BaseComponent):
17
25
defget_context_data(self, **kwargs):
18
26
context =super().get_context_data(**kwargs)
@@ -22,7 +30,11 @@ class MyComponent(BaseComponent):
22
30
return context
23
31
```
24
32
25
-
You can then use the component class in your template like this:
33
+
## Using component classes in templates
34
+
35
+
After registering your component class, you can reference it in your templates by its class name. This is done using the `component_class` parameter when invoking a component. When the template is rendered, Unfold will automatically instantiate the specified component class, execute its `get_context_data` method, and pass the resulting context to the component template.
36
+
37
+
This approach allows you to separate your data preparation logic from your presentation templates, promoting cleaner code organization and reusability. The component class handles all the data processing, while the template focuses solely on rendering the prepared data.
0 commit comments