mirror of
https://github.com/django-components/django-components.git
synced 2025-08-10 09:17:59 +00:00
refactor: update Component.render docstring
This commit is contained in:
parent
5c89d4dbeb
commit
c6e3c0a213
1 changed files with 44 additions and 4 deletions
|
@ -348,21 +348,45 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
||||||
**response_kwargs: Any,
|
**response_kwargs: Any,
|
||||||
) -> HttpResponse:
|
) -> 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
|
This is the interface for the `django.views.View` class which allows us to
|
||||||
use components as Django views with `component.as_view()`.
|
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:
|
Example:
|
||||||
```py
|
```py
|
||||||
MyComponent.render_to_response(
|
MyComponent.render_to_response(
|
||||||
args=[1, "two", {}],
|
args=[1, "two", {}],
|
||||||
kwargs={
|
kwargs={
|
||||||
"key"=123,
|
"key": 123,
|
||||||
},
|
},
|
||||||
slots={
|
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,
|
escape_slots_content=False,
|
||||||
|
# HttpResponse input
|
||||||
|
status=201,
|
||||||
|
headers={...},
|
||||||
)
|
)
|
||||||
|
# HttpResponse(content=..., status=201, headers=...)
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
content = cls.render(
|
content = cls.render(
|
||||||
|
@ -384,15 +408,31 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
||||||
escape_slots_content: bool = True,
|
escape_slots_content: bool = True,
|
||||||
) -> str:
|
) -> 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:
|
Example:
|
||||||
```py
|
```py
|
||||||
MyComponent.render(
|
MyComponent.render(
|
||||||
args=[1, "two", {}],
|
args=[1, "two", {}],
|
||||||
kwargs={
|
kwargs={
|
||||||
"key"=123,
|
"key": 123,
|
||||||
},
|
},
|
||||||
slots={
|
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,
|
escape_slots_content=False,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue