mirror of
https://github.com/django-components/django-components.git
synced 2025-11-18 22:11:26 +00:00
Deployed 49afdb49 to dev with MkDocs 1.6.1 and mike 2.1.3
This commit is contained in:
parent
01f6852b4d
commit
5c55ebeac8
188 changed files with 3598 additions and 863 deletions
34
dev/examples/recursion/component.py
Normal file
34
dev/examples/recursion/component.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from typing import NamedTuple
|
||||
|
||||
from django_components import Component, register, types
|
||||
|
||||
DESCRIPTION = "100 nested components? Not a problem! Handle recursive rendering out of the box."
|
||||
|
||||
|
||||
@register("recursion")
|
||||
class Recursion(Component):
|
||||
class Kwargs(NamedTuple):
|
||||
current_depth: int = 0
|
||||
|
||||
def get_template_data(self, args, kwargs: Kwargs, slots, context):
|
||||
current_depth = kwargs.current_depth
|
||||
return {
|
||||
"current_depth": current_depth,
|
||||
"next_depth": current_depth + 1,
|
||||
}
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
<div class="py-4 border-l-2 border-gray-300 ml-1">
|
||||
{% if current_depth < 100 %}
|
||||
<p class="text-sm text-gray-600">
|
||||
Recursion depth: {{ current_depth }}
|
||||
</p>
|
||||
{% component "recursion" current_depth=next_depth / %}
|
||||
{% else %}
|
||||
<p class="text-sm font-semibold text-green-600">
|
||||
Reached maximum recursion depth!
|
||||
</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
"""
|
||||
BIN
dev/examples/recursion/images/recursion.png
Normal file
BIN
dev/examples/recursion/images/recursion.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 85 KiB |
192
dev/examples/recursion/index.html
Normal file
192
dev/examples/recursion/index.html
Normal file
File diff suppressed because one or more lines are too long
35
dev/examples/recursion/page.py
Normal file
35
dev/examples/recursion/page.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
from django.http import HttpRequest, HttpResponse
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from django_components import Component, types
|
||||
|
||||
|
||||
class RecursionPage(Component):
|
||||
class Media:
|
||||
js = (
|
||||
mark_safe(
|
||||
'<script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,line-clamp,container-queries"></script>'
|
||||
),
|
||||
)
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
<html>
|
||||
<head>
|
||||
<title>Recursion Example</title>
|
||||
</head>
|
||||
<body class="bg-gray-100 p-8">
|
||||
<div class="max-w-4xl mx-auto bg-white p-6 rounded-lg shadow-md">
|
||||
<h1 class="text-2xl font-bold mb-4">Recursion</h1>
|
||||
<p class="text-gray-600 mb-6">
|
||||
Django components easily handles even deeply nested components.
|
||||
</p>
|
||||
{% component "recursion" / %}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
class View:
|
||||
def get(self, request: HttpRequest) -> HttpResponse:
|
||||
return RecursionPage.render_to_response(request=request)
|
||||
29
dev/examples/recursion/test_example_recursive.py
Normal file
29
dev/examples/recursion/test_example_recursive.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import pytest
|
||||
from django.template import Context, Template
|
||||
|
||||
from django_components import registry, types
|
||||
from django_components.testing import djc_test
|
||||
|
||||
|
||||
def _import_components():
|
||||
from docs.examples.recursion.component import Recursion # noqa: PLC0415
|
||||
|
||||
registry.register("recursion", Recursion)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@djc_test
|
||||
class TestRecursion:
|
||||
def test_renders_recursively(self):
|
||||
_import_components()
|
||||
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
{% component "recursion" / %}
|
||||
"""
|
||||
template = Template(template_str)
|
||||
rendered = template.render(Context({}))
|
||||
|
||||
# Expect 101 levels of depth (0 to 100)
|
||||
assert rendered.count("Recursion depth:") == 100
|
||||
assert "Reached maximum recursion depth!" in rendered
|
||||
Loading…
Add table
Add a link
Reference in a new issue