From d4d834256a0faaa47c6ddbb590da504bcb958037 Mon Sep 17 00:00:00 2001 From: Juro Oravec Date: Sun, 4 May 2025 01:49:54 +0200 Subject: [PATCH] refactor: rename `context_data` field to `template_data` (#1162) --- CHANGELOG.md | 20 ++++++++++++++++++++ docs/reference/extension_hooks.md | 3 ++- docs/reference/template_tags.md | 2 +- docs/scripts/reference.py | 3 +++ src/django_components/component.py | 2 ++ src/django_components/extension.py | 5 ++++- tests/test_extension.py | 2 +- 7 files changed, 33 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84419519..3b0d0e69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -194,6 +194,26 @@ - `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 - New method to render template variables - `get_template_data()` diff --git a/docs/reference/extension_hooks.md b/docs/reference/extension_hooks.md index 0c241514..39a49bd9 100644 --- a/docs/reference/extension_hooks.md +++ b/docs/reference/extension_hooks.md @@ -61,9 +61,10 @@ name | type | description `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_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()` `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 options: diff --git a/docs/reference/template_tags.md b/docs/reference/template_tags.md index 669a8936..91e10fd3 100644 --- a/docs/reference/template_tags.md +++ b/docs/reference/template_tags.md @@ -67,7 +67,7 @@ If you insert this tag multiple times, ALL JS scripts will be duplicately insert -See source code +See source code diff --git a/docs/scripts/reference.py b/docs/scripts/reference.py index e3f791b8..4a77df54 100644 --- a/docs/scripts/reference.py +++ b/docs/scripts/reference.py @@ -873,6 +873,9 @@ def _extract_property_docstrings(cls: Type) -> Dict[str, str]: if line.endswith("):\n"): ignore = False continue + # Ignore comments + elif line.strip().startswith("#"): + continue else: attrs_lines.append(line) diff --git a/src/django_components/component.py b/src/django_components/component.py index 0d8751c5..faeed9e0 100644 --- a/src/django_components/component.py +++ b/src/django_components/component.py @@ -2476,7 +2476,9 @@ class Component(metaclass=ComponentMeta): component=self, component_cls=self.__class__, component_id=render_id, + # TODO_V1 - Remove `context_data` context_data=template_data, + template_data=template_data, js_data=js_data, css_data=css_data, ) diff --git a/src/django_components/extension.py b/src/django_components/extension.py index 7378b0f2..86ce0b57 100644 --- a/src/django_components/extension.py +++ b/src/django_components/extension.py @@ -104,8 +104,11 @@ class OnComponentDataContext(NamedTuple): """The Component class""" component_id: str """The unique identifier for this component instance""" + # TODO_V1 - Remove `context_data` 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 """Dictionary of JavaScript data from `Component.get_js_data()`""" css_data: Dict diff --git a/tests/test_extension.py b/tests/test_extension.py index 76fce84d..904f7f9d 100644 --- a/tests/test_extension.py +++ b/tests/test_extension.py @@ -300,7 +300,7 @@ class TestExtensionHooks: data_call: OnComponentDataContext = extension.calls["on_component_data"][0] assert data_call.component_cls == TestComponent 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.css_data == {"style": "body { color: blue; }"}