Skip to content

Commit 79337e1

Browse files
committed
Update docs for __init__ vs mount.
1 parent 0b737f9 commit 79337e1

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

docs/source/components.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,32 @@ If there are multiple of the same components on the page, a `key` kwarg can be p
3333

3434
## Component arguments
3535

36-
`kwargs` can be passed into the `unicorn` templatetag from the template. The `kwargs` will be available in the component [`__init__`](advanced.md#__init__) method.
36+
`args` and `kwargs` can be passed into the `unicorn` templatetag from the template. They will be available in the component [`component_args`](advanced.md#component_args) and [`component_kwargs`](advanced.md#component_kwargs) methods respectively.
3737

38-
```{warning}
39-
When overriding `__init__` calling `super().__init__(**kwargs)` is required for the component to initialize properly.
38+
```html
39+
<!-- index.html -->
40+
{% unicorn 'hello-world' "Hello" name="World" %}
4041
```
4142

4243
```python
4344
# hello_world.py
4445
from django_unicorn.components import UnicornView
4546

4647
class HelloWorldView(UnicornView):
47-
name = "World"
48+
def mount(self):
49+
arg = self.component_args[0]
50+
kwarg = self.component_kwargs["name"]
4851

49-
def __init__(self, *args, **kwargs):
50-
super().__init__(**kwargs) # calling super is required
51-
self.name = kwargs.get("name")
52+
assert f"{arg} {kwarg}" == "Hello World"
5253
```
5354

55+
Regular Django template variables can also be passed in as an argument as long as it is available in the template context.
56+
5457
```html
5558
<!-- index.html -->
56-
{% unicorn 'hello-world' name="Universe" %}
59+
{% unicorn 'hello-world' name=hello.world.name %}
5760
```
5861

59-
Regular Django template variables can also be passed in as an argument as long as it is available in the template context.
60-
6162
```python
6263
# views.py
6364
from django.shortcuts import render
@@ -67,9 +68,12 @@ def index(request):
6768
return render(request, "index.html", context)
6869
```
6970

70-
```html
71-
<!-- index.html -->
72-
{% unicorn 'hello-world' name=hello.world.name %}
71+
```python
72+
class HelloWorldView(UnicornView):
73+
def mount(self):
74+
kwarg = self.component_kwargs["name"]
75+
76+
assert kwarg == "Galaxy"
7377
```
7478

7579
## Example component
@@ -222,7 +226,7 @@ Another option is to set the `form_class` on the component and utilize Django's
222226
from django_unicorn.components import UnicornView, UnicornField
223227
224228
class Author(UnicornField):
225-
def __init__(self):
229+
def mount(self):
226230
self.name = 'Neil Gaiman'
227231
228232
# Not needed because inherited from `UnicornField`

0 commit comments

Comments
 (0)