refactor: update Component.render docstring

This commit is contained in:
Juro Oravec 2024-06-11 15:06:14 +02:00
parent 5c89d4dbeb
commit c6e3c0a213

View file

@ -348,21 +348,45 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
**response_kwargs: Any,
) -> HttpResponse:
"""
Render the component and wrap the content in the response class.
The response class is taken from `Component.response_class`. Defaults to `django.http.HttpResponse`.
This is the interface for the `django.views.View` class which allows us to
use components as Django views with `component.as_view()`.
Inputs:
- `args` - Positional args for the component. This is the same as calling the component
as `{% component "my_comp" arg1 arg2 ... %}`
- `kwargs` - Kwargs for the component. This is the same as calling the component
as `{% component "my_comp" key1=val1 key2=val2 ... %}`
- `slots` - Component slot fills. This is the same as pasing `{% fill %}` tags to the component.
Accepts a dictionary of `{ slot_name: slot_content }` where `slot_content` can be a string or render function.
- `escape_slots_content` - Whether the content from `slots` should be escaped.
- `context` - A context (dictionary or Django's Context) within which the component
is rendered. The keys on the context can be accessed from within the template.
- NOTE: In "isolated" mode, context is NOT accessible, and data MUST be passed via
component's args and kwargs.
Any additional args and kwargs are passed to the `response_class`.
Example:
```py
MyComponent.render_to_response(
args=[1, "two", {}],
kwargs={
"key"=123,
"key": 123,
},
slots={
header='STATIC TEXT HERE',
"header": 'STATIC TEXT HERE',
"footer": lambda ctx, slot_kwargs, slot_ref: f'CTX: {ctx['hello']} SLOT_DATA: {slot_kwargs['abc']}',
},
escape_slots_content=False,
# HttpResponse input
status=201,
headers={...},
)
# HttpResponse(content=..., status=201, headers=...)
```
"""
content = cls.render(
@ -384,15 +408,31 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
escape_slots_content: bool = True,
) -> str:
"""
Render the component into a string.
Inputs:
- `args` - Positional args for the component. This is the same as calling the component
as `{% component "my_comp" arg1 arg2 ... %}`
- `kwargs` - Kwargs for the component. This is the same as calling the component
as `{% component "my_comp" key1=val1 key2=val2 ... %}`
- `slots` - Component slot fills. This is the same as pasing `{% fill %}` tags to the component.
Accepts a dictionary of `{ slot_name: slot_content }` where `slot_content` can be a string or render function.
- `escape_slots_content` - Whether the content from `slots` should be escaped.
- `context` - A context (dictionary or Django's Context) within which the component
is rendered. The keys on the context can be accessed from within the template.
- NOTE: In "isolated" mode, context is NOT accessible, and data MUST be passed via
component's args and kwargs.
Example:
```py
MyComponent.render(
args=[1, "two", {}],
kwargs={
"key"=123,
"key": 123,
},
slots={
header='STATIC TEXT HERE',
"header": 'STATIC TEXT HERE',
"footer": lambda ctx, slot_kwargs, slot_ref: f'CTX: {ctx['hello']} SLOT_DATA: {slot_kwargs['abc']}',
},
escape_slots_content=False,
)