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/source/components.md
+18-14Lines changed: 18 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,31 +33,32 @@ If there are multiple of the same components on the page, a `key` kwarg can be p
33
33
34
34
## Component arguments
35
35
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.
37
37
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" %}
40
41
```
41
42
42
43
```python
43
44
# hello_world.py
44
45
from django_unicorn.components import UnicornView
45
46
46
47
classHelloWorldView(UnicornView):
47
-
name ="World"
48
+
defmount(self):
49
+
arg =self.component_args[0]
50
+
kwarg =self.component_kwargs["name"]
48
51
49
-
def__init__(self, *args, **kwargs):
50
-
super().__init__(**kwargs) # calling super is required
51
-
self.name = kwargs.get("name")
52
+
assertf"{arg}{kwarg}"=="Hello World"
52
53
```
53
54
55
+
Regular Django template variables can also be passed in as an argument as long as it is available in the template context.
56
+
54
57
```html
55
58
<!-- index.html -->
56
-
{% unicorn 'hello-world' name="Universe" %}
59
+
{% unicorn 'hello-world' name=hello.world.name %}
57
60
```
58
61
59
-
Regular Django template variables can also be passed in as an argument as long as it is available in the template context.
60
-
61
62
```python
62
63
# views.py
63
64
from django.shortcuts import render
@@ -67,9 +68,12 @@ def index(request):
67
68
return render(request, "index.html", context)
68
69
```
69
70
70
-
```html
71
-
<!-- index.html -->
72
-
{% unicorn 'hello-world' name=hello.world.name %}
71
+
```python
72
+
classHelloWorldView(UnicornView):
73
+
defmount(self):
74
+
kwarg =self.component_kwargs["name"]
75
+
76
+
assert kwarg =="Galaxy"
73
77
```
74
78
75
79
## Example component
@@ -222,7 +226,7 @@ Another option is to set the `form_class` on the component and utilize Django's
222
226
from django_unicorn.components import UnicornView, UnicornField
223
227
224
228
class Author(UnicornField):
225
-
def __init__(self):
229
+
def mount(self):
226
230
self.name = 'Neil Gaiman'
227
231
228
232
# Not needed because inherited from `UnicornField`
0 commit comments