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
|
@ -100,6 +100,44 @@ class ProfileCard(Component):
|
|||
}
|
||||
```
|
||||
|
||||
There is a slight difference between [`get_context_data()`](../../../reference/api/#django_components.Component.get_context_data) and [`get_template_data()`](../../../reference/api/#django_components.Component.get_template_data)
|
||||
when rendering a component with the [`{% component %}`](../../../reference/template_tags/#component) tag.
|
||||
|
||||
For example if you have component that accepts kwarg `date`:
|
||||
|
||||
```py
|
||||
class MyComponent(Component):
|
||||
def get_context_data(self, date, *args, **kwargs):
|
||||
return {
|
||||
"date": date,
|
||||
}
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"date": kwargs["date"],
|
||||
}
|
||||
```
|
||||
|
||||
The difference is that:
|
||||
|
||||
- With [`get_context_data()`](../../../reference/api/#django_components.Component.get_context_data), you can pass `date` either as arg or kwarg:
|
||||
|
||||
```django
|
||||
✅
|
||||
{% component "my_component" date=some_date %}
|
||||
{% component "my_component" some_date %}
|
||||
```
|
||||
|
||||
- But with [`get_template_data()`](../../../reference/api/#django_components.Component.get_template_data), `date` MUST be passed as kwarg:
|
||||
|
||||
```django
|
||||
✅
|
||||
{% component "my_component" date=some_date %}
|
||||
|
||||
❌
|
||||
{% component "my_component" some_date %}
|
||||
```
|
||||
|
||||
!!! warning
|
||||
|
||||
[`get_template_data()`](../../../reference/api/#django_components.Component.get_template_data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue