mirror of
https://github.com/django-components/django-components.git
synced 2025-08-03 22:08: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
|
@ -1,3 +1,5 @@
|
|||
from typing import NamedTuple
|
||||
|
||||
from django_components import Component, register
|
||||
|
||||
|
||||
|
@ -13,9 +15,12 @@ class Calendar(Component):
|
|||
js_file = "calendar/calendar.js"
|
||||
|
||||
# This component takes one parameter, a date string to show in the template
|
||||
def get_context_data(self, date):
|
||||
class Kwargs(NamedTuple):
|
||||
date: str
|
||||
|
||||
def get_template_data(self, args, kwargs: Kwargs, slots, context):
|
||||
return {
|
||||
"date": date,
|
||||
"date": kwargs.date,
|
||||
}
|
||||
|
||||
class View:
|
||||
|
@ -41,9 +46,12 @@ class CalendarRelative(Component):
|
|||
js_file = "calendar.js"
|
||||
|
||||
# This component takes one parameter, a date string to show in the template
|
||||
def get_context_data(self, date):
|
||||
class Kwargs(NamedTuple):
|
||||
date: str
|
||||
|
||||
def get_template_data(self, args, kwargs: Kwargs, slots, context):
|
||||
return {
|
||||
"date": date,
|
||||
"date": kwargs.date,
|
||||
}
|
||||
|
||||
class View:
|
||||
|
|
|
@ -1,24 +1,8 @@
|
|||
from typing import Any, Dict
|
||||
|
||||
from django_components import Component, register, types
|
||||
|
||||
|
||||
@register("greeting")
|
||||
class Greeting(Component):
|
||||
class View:
|
||||
def get(self, request, *args, **kwargs):
|
||||
slots = {"message": "Hello, world!"}
|
||||
return Greeting.render_to_response(
|
||||
request=request,
|
||||
slots=slots,
|
||||
kwargs={
|
||||
"name": request.GET.get("name", ""),
|
||||
},
|
||||
)
|
||||
|
||||
def get_context_data(self, name, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"name": name}
|
||||
|
||||
template: types.django_html = """
|
||||
<div id="greeting">Hello, {{ name }}!</div>
|
||||
{% slot "message" %}{% endslot %}
|
||||
|
@ -37,3 +21,17 @@ class Greeting(Component):
|
|||
alert("Hello!");
|
||||
});
|
||||
"""
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"name": kwargs["name"]}
|
||||
|
||||
class View:
|
||||
def get(self, request, *args, **kwargs):
|
||||
slots = {"message": "Hello, world!"}
|
||||
return Greeting.render_to_response(
|
||||
request=request,
|
||||
slots=slots,
|
||||
kwargs={
|
||||
"name": request.GET.get("name", ""),
|
||||
},
|
||||
)
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from typing import NamedTuple
|
||||
|
||||
from django_components import Component, register
|
||||
|
||||
|
||||
|
@ -13,9 +15,12 @@ class CalendarNested(Component):
|
|||
js_file = "calendar.js"
|
||||
|
||||
# This component takes one parameter, a date string to show in the template
|
||||
def get_context_data(self, date):
|
||||
class Kwargs(NamedTuple):
|
||||
date: str
|
||||
|
||||
def get_template_data(self, args, kwargs: Kwargs, slots, context):
|
||||
return {
|
||||
"date": date,
|
||||
"date": kwargs.date,
|
||||
}
|
||||
|
||||
class View:
|
||||
|
|
|
@ -1,27 +1,11 @@
|
|||
import time
|
||||
from typing import Any, Dict
|
||||
from typing import NamedTuple
|
||||
|
||||
from django_components import Component, register, types
|
||||
|
||||
|
||||
@register("recursive")
|
||||
class Recursive(Component):
|
||||
class View:
|
||||
def get(self, request):
|
||||
time_before = time.time()
|
||||
output = Recursive.render_to_response(
|
||||
request=request,
|
||||
kwargs={
|
||||
"depth": 0,
|
||||
},
|
||||
)
|
||||
time_after = time.time()
|
||||
print("TIME: ", time_after - time_before)
|
||||
return output
|
||||
|
||||
def get_context_data(self, depth: int = 0) -> Dict[str, Any]:
|
||||
return {"depth": depth + 1}
|
||||
|
||||
template: types.django_html = """
|
||||
<div id="recursive">
|
||||
depth: {{ depth }}
|
||||
|
@ -31,3 +15,25 @@ class Recursive(Component):
|
|||
{% endif %}
|
||||
</div>
|
||||
"""
|
||||
|
||||
class Kwargs(NamedTuple):
|
||||
depth: int
|
||||
|
||||
class Defaults:
|
||||
depth = 0
|
||||
|
||||
def get_template_data(self, args, kwargs: Kwargs, slots, context):
|
||||
return {"depth": kwargs.depth + 1}
|
||||
|
||||
class View:
|
||||
def get(self, request):
|
||||
time_before = time.time()
|
||||
output = Recursive.render_to_response(
|
||||
request=request,
|
||||
kwargs=Recursive.Kwargs(
|
||||
depth=0,
|
||||
),
|
||||
)
|
||||
time_after = time.time()
|
||||
print("TIME: ", time_after - time_before)
|
||||
return output
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue