refactor: rename context_data field to template_data (#1162)

This commit is contained in:
Juro Oravec 2025-05-04 01:49:54 +02:00 committed by GitHub
parent 28b61c1609
commit d4d834256a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 33 additions and 4 deletions

View file

@ -194,6 +194,26 @@
- `SlotContent` was renamed to `SlotInput`. The old name is deprecated and will be removed in v1. - `SlotContent` was renamed to `SlotInput`. The old name is deprecated and will be removed in v1.
- In the `on_component_data()` extension hook, the `context_data` field of the context object was superseded by `template_data`.
The `context_data` field will be removed in v1.0.
Before:
```py
class MyExtension(ComponentExtension):
def on_component_data(self, ctx: OnComponentDataContext) -> None:
ctx.context_data["my_template_var"] = "my_value"
```
After:
```py
class MyExtension(ComponentExtension):
def on_component_data(self, ctx: OnComponentDataContext) -> None:
ctx.template_data["my_template_var"] = "my_value"
```
#### Feat #### Feat
- New method to render template variables - `get_template_data()` - New method to render template variables - `get_template_data()`

View file

@ -61,9 +61,10 @@ name | type | description
`component` | [`Component`](../api#django_components.Component) | The Component instance that is being rendered `component` | [`Component`](../api#django_components.Component) | The Component instance that is being rendered
`component_cls` | [`Type[Component]`](../api#django_components.Component) | The Component class `component_cls` | [`Type[Component]`](../api#django_components.Component) | The Component class
`component_id` | `str` | The unique identifier for this component instance `component_id` | `str` | The unique identifier for this component instance
`context_data` | `Dict` | Dictionary of context data from `Component.get_context_data()` `context_data` | `Dict` | Deprecated. Use `template_data` instead. Will be removed in v1.0.
`css_data` | `Dict` | Dictionary of CSS data from `Component.get_css_data()` `css_data` | `Dict` | Dictionary of CSS data from `Component.get_css_data()`
`js_data` | `Dict` | Dictionary of JavaScript data from `Component.get_js_data()` `js_data` | `Dict` | Dictionary of JavaScript data from `Component.get_js_data()`
`template_data` | `Dict` | Dictionary of template data from `Component.get_template_data()`
::: django_components.extension.ComponentExtension.on_component_input ::: django_components.extension.ComponentExtension.on_component_input
options: options:

View file

@ -67,7 +67,7 @@ If you insert this tag multiple times, ALL JS scripts will be duplicately insert
<a href="https://github.com/django-components/django-components/tree/master/src/django_components/templatetags/component_tags.py#L2772" target="_blank">See source code</a> <a href="https://github.com/django-components/django-components/tree/master/src/django_components/templatetags/component_tags.py#L2791" target="_blank">See source code</a>

View file

@ -873,6 +873,9 @@ def _extract_property_docstrings(cls: Type) -> Dict[str, str]:
if line.endswith("):\n"): if line.endswith("):\n"):
ignore = False ignore = False
continue continue
# Ignore comments
elif line.strip().startswith("#"):
continue
else: else:
attrs_lines.append(line) attrs_lines.append(line)

View file

@ -2476,7 +2476,9 @@ class Component(metaclass=ComponentMeta):
component=self, component=self,
component_cls=self.__class__, component_cls=self.__class__,
component_id=render_id, component_id=render_id,
# TODO_V1 - Remove `context_data`
context_data=template_data, context_data=template_data,
template_data=template_data,
js_data=js_data, js_data=js_data,
css_data=css_data, css_data=css_data,
) )

View file

@ -104,8 +104,11 @@ class OnComponentDataContext(NamedTuple):
"""The Component class""" """The Component class"""
component_id: str component_id: str
"""The unique identifier for this component instance""" """The unique identifier for this component instance"""
# TODO_V1 - Remove `context_data`
context_data: Dict context_data: Dict
"""Dictionary of context data from `Component.get_context_data()`""" """Deprecated. Use `template_data` instead. Will be removed in v1.0."""
template_data: Dict
"""Dictionary of template data from `Component.get_template_data()`"""
js_data: Dict js_data: Dict
"""Dictionary of JavaScript data from `Component.get_js_data()`""" """Dictionary of JavaScript data from `Component.get_js_data()`"""
css_data: Dict css_data: Dict

View file

@ -300,7 +300,7 @@ class TestExtensionHooks:
data_call: OnComponentDataContext = extension.calls["on_component_data"][0] data_call: OnComponentDataContext = extension.calls["on_component_data"][0]
assert data_call.component_cls == TestComponent assert data_call.component_cls == TestComponent
assert isinstance(data_call.component_id, str) assert isinstance(data_call.component_id, str)
assert data_call.context_data == {"name": "Test"} assert data_call.template_data == {"name": "Test"}
assert data_call.js_data == {"script": "console.log('Hello!')"} assert data_call.js_data == {"script": "console.log('Hello!')"}
assert data_call.css_data == {"style": "body { color: blue; }"} assert data_call.css_data == {"style": "body { color: blue; }"}