mirror of
https://github.com/django-components/django-components.git
synced 2025-08-04 06:18:17 +00:00
refactor: Update docs and tests to use get_template_data() (#1161)
* refactor: update docs and tests to use get_template_data() * refactor: fix linting * docs: add note about difference between the two methods
This commit is contained in:
parent
c69980493d
commit
28b61c1609
69 changed files with 795 additions and 725 deletions
|
@ -98,8 +98,8 @@ class MyComponent(Component):
|
|||
ttl = 300 # Cache for 5 minutes
|
||||
cache_name = "my_cache"
|
||||
|
||||
def get_context_data(self, name, **kwargs):
|
||||
return {"name": name}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"name": kwargs["name"]}
|
||||
```
|
||||
|
||||
In this example, the component's rendered output is cached for 5 minutes using the `my_cache` backend.
|
||||
|
|
|
@ -12,7 +12,7 @@ NOTE: `{% csrf_token %}` tags need access to the top-level context, and they wil
|
|||
|
||||
If you find yourself using the `only` modifier often, you can set the [context_behavior](#context-behavior) option to `"isolated"`, which automatically applies the `only` modifier. This is useful if you want to make sure that components don't accidentally access the outer context.
|
||||
|
||||
Components can also access the outer context in their context methods like `get_context_data` by accessing the property `self.outer_context`.
|
||||
Components can also access the outer context in their context methods like `get_template_data` by accessing the property `self.outer_context`.
|
||||
|
||||
## Example of Accessing Outer Context
|
||||
|
||||
|
@ -22,14 +22,14 @@ Components can also access the outer context in their context methods like `get_
|
|||
</div>
|
||||
```
|
||||
|
||||
Assuming that the rendering context has variables such as `date`, you can use `self.outer_context` to access them from within `get_context_data`. Here's how you might implement it:
|
||||
Assuming that the rendering context has variables such as `date`, you can use `self.outer_context` to access them from within `get_template_data`. Here's how you might implement it:
|
||||
|
||||
```python
|
||||
class Calender(Component):
|
||||
|
||||
...
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
outer_field = self.outer_context["date"]
|
||||
return {
|
||||
"date": outer_fields,
|
||||
|
@ -56,7 +56,7 @@ This has two modes:
|
|||
[`{% with %}`](https://docs.djangoproject.com/en/5.1/ref/templates/builtins/#with) tags.
|
||||
- Any loops ([`{% for ... %}`](https://docs.djangoproject.com/en/5.1/ref/templates/builtins/#cycle))
|
||||
that the `{% fill %}` tag is part of.
|
||||
- Data returned from [`Component.get_context_data()`](../../../reference/api#django_components.Component.get_context_data)
|
||||
- Data returned from [`Component.get_template_data()`](../../../reference/api#django_components.Component.get_template_data)
|
||||
of the component that owns the fill tag.
|
||||
|
||||
- `"isolated"`
|
||||
|
@ -69,12 +69,12 @@ This has two modes:
|
|||
|
||||
- Any loops ([`{% for ... %}`](https://docs.djangoproject.com/en/5.1/ref/templates/builtins/#cycle))
|
||||
that the `{% fill %}` tag is part of.
|
||||
- [`Component.get_context_data()`](../../../reference/api#django_components.Component.get_context_data)
|
||||
- [`Component.get_template_data()`](../../../reference/api#django_components.Component.get_template_data)
|
||||
of the component which defined the template (AKA the "root" component).
|
||||
|
||||
!!! warning
|
||||
|
||||
Notice that the component whose `get_context_data()` we use inside
|
||||
Notice that the component whose `get_template_data()` we use inside
|
||||
[`{% fill %}`](../../../reference/template_tags#fill)
|
||||
is NOT the same across the two modes!
|
||||
|
||||
|
@ -93,10 +93,10 @@ This has two modes:
|
|||
"""
|
||||
```
|
||||
|
||||
- `"django"` - `my_var` has access to data from `get_context_data()` of both `Inner` and `Outer`.
|
||||
- `"django"` - `my_var` has access to data from `get_template_data()` of both `Inner` and `Outer`.
|
||||
If there are variables defined in both, then `Inner` overshadows `Outer`.
|
||||
|
||||
- `"isolated"` - `my_var` has access to data from `get_context_data()` of ONLY `Outer`.
|
||||
- `"isolated"` - `my_var` has access to data from `get_template_data()` of ONLY `Outer`.
|
||||
|
||||
|
||||
### Example "django"
|
||||
|
@ -115,11 +115,11 @@ class RootComp(Component):
|
|||
{% endwith %}
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return { "my_var": 123 }
|
||||
```
|
||||
|
||||
Then if [`get_context_data()`](../../../reference/api#django_components.Component.get_context_data)
|
||||
Then if [`get_template_data()`](../../../reference/api#django_components.Component.get_template_data)
|
||||
of the component `"my_comp"` returns following data:
|
||||
|
||||
```py
|
||||
|
@ -154,11 +154,11 @@ class RootComp(Component):
|
|||
{% endwith %}
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return { "my_var": 123 }
|
||||
```
|
||||
|
||||
Then if [`get_context_data()`](../../../reference/api#django_components.Component.get_context_data)
|
||||
Then if [`get_template_data()`](../../../reference/api#django_components.Component.get_template_data)
|
||||
of the component `"my_comp"` returns following data:
|
||||
|
||||
```py
|
||||
|
@ -172,7 +172,7 @@ Then the template will be rendered as:
|
|||
# cheese
|
||||
```
|
||||
|
||||
Because variables `"my_var"` and `"cheese"` are searched only inside `RootComponent.get_context_data()`.
|
||||
Because variables `"my_var"` and `"cheese"` are searched only inside `RootComponent.get_template_data()`.
|
||||
But since `"cheese"` is not defined there, it's empty.
|
||||
|
||||
!!! info
|
||||
|
|
|
@ -12,9 +12,9 @@ class Calendar(Component):
|
|||
template_file = "template.html"
|
||||
|
||||
# This component takes one parameter, a date string to show in the template
|
||||
def get_context_data(self, date):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"date": date,
|
||||
"date": kwargs["date"],
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ Extensions can define methods to hook into lifecycle events, such as:
|
|||
- Un/registering a component
|
||||
- Creating or deleting a registry
|
||||
- Pre-processing data passed to a component on render
|
||||
- Post-processing data returned from [`get_context_data()`](../../../reference/api#django_components.Component.get_context_data)
|
||||
- Post-processing data returned from [`get_template_data()`](../../../reference/api#django_components.Component.get_template_data)
|
||||
and others.
|
||||
|
||||
See the full list in [Extension Hooks Reference](../../../reference/extension_hooks).
|
||||
|
@ -122,7 +122,7 @@ For example, the View extension is available as `self.view`:
|
|||
|
||||
```python
|
||||
class MyTable(Component):
|
||||
def get_context_data(self, request):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
# `self.view` points to the instance of `View` extension.
|
||||
return {
|
||||
"view": self.view,
|
||||
|
@ -133,7 +133,7 @@ And the Storybook extension is available as `self.storybook`:
|
|||
|
||||
```python
|
||||
class MyTable(Component):
|
||||
def get_context_data(self, request):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
# `self.storybook` points to the instance of `Storybook` extension.
|
||||
return {
|
||||
"title": self.storybook.title(),
|
||||
|
|
|
@ -78,7 +78,7 @@ In the example from previous section, we've defined two kwargs: `hello="hi" anot
|
|||
|
||||
```py
|
||||
class ChildComponent(Component):
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
my_data = self.inject("my_data")
|
||||
print(my_data.hello) # hi
|
||||
print(my_data.another) # 123
|
||||
|
@ -94,7 +94,7 @@ To avoid the error, you can pass a second argument to [`inject()`](../../../refe
|
|||
|
||||
```py
|
||||
class ChildComponent(Component):
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
my_data = self.inject("invalid_key", DEFAULT_DATA)
|
||||
assert my_data == DEFAULT_DATA
|
||||
return {}
|
||||
|
@ -119,7 +119,7 @@ class ChildComponent(Component):
|
|||
<div> {{ my_data.another }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
my_data = self.inject("my_data", "default")
|
||||
return {"my_data": my_data}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue