mirror of
https://github.com/django-components/django-components.git
synced 2025-07-08 01:45:00 +00:00

* refactor: update docs and tests to use get_template_data() * refactor: fix linting * docs: add note about difference between the two methods
39 lines
1,003 B
Python
39 lines
1,003 B
Python
import time
|
|
from typing import NamedTuple
|
|
|
|
from django_components import Component, register, types
|
|
|
|
|
|
@register("recursive")
|
|
class Recursive(Component):
|
|
template: types.django_html = """
|
|
<div id="recursive">
|
|
depth: {{ depth }}
|
|
<hr/>
|
|
{% if depth <= 100 %}
|
|
{% component "recursive" depth=depth / %}
|
|
{% 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
|