django-components 0.94__tar.gz → 0.95__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {django_components-0.94/src/django_components.egg-info → django_components-0.95}/PKG-INFO +67 -3
- {django_components-0.94 → django_components-0.95}/README.md +66 -2
- {django_components-0.94 → django_components-0.95}/pyproject.toml +1 -1
- {django_components-0.94 → django_components-0.95}/src/django_components/__init__.py +1 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/app_settings.py +5 -1
- {django_components-0.94 → django_components-0.95}/src/django_components/apps.py +17 -12
- {django_components-0.94 → django_components-0.95}/src/django_components/component.py +7 -2
- {django_components-0.94 → django_components-0.95}/src/django_components/component_registry.py +9 -1
- django_components-0.95/src/django_components/components/__init__.py +3 -0
- django_components-0.95/src/django_components/components/dynamic.py +89 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/slots.py +8 -2
- {django_components-0.94 → django_components-0.95/src/django_components.egg-info}/PKG-INFO +67 -3
- {django_components-0.94 → django_components-0.95}/src/django_components.egg-info/SOURCES.txt +2 -0
- {django_components-0.94 → django_components-0.95}/tests/test_templatetags_component.py +298 -14
- {django_components-0.94 → django_components-0.95}/LICENSE +0 -0
- {django_components-0.94 → django_components-0.95}/setup.cfg +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/attributes.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/autodiscover.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/component_media.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/context.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/expression.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/library.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/logger.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/management/__init__.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/management/commands/__init__.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/management/commands/startcomponent.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/management/commands/upgradecomponent.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/middleware.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/node.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/provide.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/py.typed +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/safer_staticfiles/__init__.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/safer_staticfiles/apps.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/tag_formatter.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/template_loader.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/template_parser.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/templatetags/__init__.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/templatetags/component_tags.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/types.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components/utils.py +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components.egg-info/requires.txt +0 -0
- {django_components-0.94 → django_components-0.95}/src/django_components.egg-info/top_level.txt +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_attributes.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_autodiscover.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_command_startcomponent.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_component.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_component_as_view.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_component_media.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_context.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_dependency_rendering.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_expression.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_registry.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_safer_staticfiles.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_settings.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_tag_formatter.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_template_loader.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_template_parser.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_templatetags.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_templatetags_provide.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_templatetags_slot_fill.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_templatetags_templating.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/test_utils.py +0 -0
- {django_components-0.94 → django_components-0.95}/tests/testutils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.95
|
|
4
4
|
Summary: A way to create simple reusable template components in Django.
|
|
5
5
|
Author-email: Emil Stenström <emil@emilstenstrom.se>
|
|
6
6
|
License: MIT
|
|
@@ -68,8 +68,9 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
68
68
|
- [Using single-file components](#using-single-file-components)
|
|
69
69
|
- [Use components in templates](#use-components-in-templates)
|
|
70
70
|
- [Use components outside of templates](#use-components-outside-of-templates)
|
|
71
|
-
- [Registering components](#registering-components)
|
|
72
71
|
- [Use components as views](#use-components-as-views)
|
|
72
|
+
- [Pre-defined components](#pre-defined-components)
|
|
73
|
+
- [Registering components](#registering-components)
|
|
73
74
|
- [Autodiscovery](#autodiscovery)
|
|
74
75
|
- [Using slots in templates](#using-slots-in-templates)
|
|
75
76
|
- [Accessing data passed to the component](#accessing-data-passed-to-the-component)
|
|
@@ -91,6 +92,10 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
91
92
|
|
|
92
93
|
## Release notes
|
|
93
94
|
|
|
95
|
+
**Version 0.95**
|
|
96
|
+
- Added support for dynamic components, where the component name is passed as a variable. (See [Dynamic components](#dynamic-components))
|
|
97
|
+
- Changed `Component.input` to raise `RuntimeError` if accessed outside of render context. Previously it returned `None` if unset.
|
|
98
|
+
|
|
94
99
|
**Version 0.94**
|
|
95
100
|
- django_components now automatically configures Django to support multi-line tags. (See [Multi-line tags](#multi-line-tags))
|
|
96
101
|
- New setting `reload_on_template_change`. Set this to `True` to reload the dev server on changes to component template files. (See [Reload dev server on component file changes](#reload-dev-server-on-component-file-changes))
|
|
@@ -874,6 +879,54 @@ class MyComponent(Component):
|
|
|
874
879
|
do_something_extra(request, *args, **kwargs)
|
|
875
880
|
```
|
|
876
881
|
|
|
882
|
+
## Pre-defined components
|
|
883
|
+
|
|
884
|
+
### Dynamic components
|
|
885
|
+
|
|
886
|
+
If you are writing something like a form component, you may design it such that users
|
|
887
|
+
give you the component names, and your component renders it.
|
|
888
|
+
|
|
889
|
+
While you can handle this with a series of if / else statements, this is not an extensible solution.
|
|
890
|
+
|
|
891
|
+
Instead, you can use **dynamic components**. Dynamic components are used in place of normal components.
|
|
892
|
+
|
|
893
|
+
```django
|
|
894
|
+
{% load component_tags %}
|
|
895
|
+
{% component "dynamic" is=component_name title="Cat Museum" %}
|
|
896
|
+
{% fill "content" %}
|
|
897
|
+
HELLO_FROM_SLOT_1
|
|
898
|
+
{% endfill %}
|
|
899
|
+
{% fill "sidebar" %}
|
|
900
|
+
HELLO_FROM_SLOT_2
|
|
901
|
+
{% endfill %}
|
|
902
|
+
{% endcomponent %}
|
|
903
|
+
```
|
|
904
|
+
|
|
905
|
+
These behave same way as regular components. You pass it the same args, kwargs, and slots as you would
|
|
906
|
+
to the component that you want to render.
|
|
907
|
+
|
|
908
|
+
The only exception is that also you supply 1-2 additional inputs:
|
|
909
|
+
- `is` - Required - The component name or a component class to render
|
|
910
|
+
- `registry` - Optional - The `ComponentRegistry` that will be searched if `is` is a component name. If omitted, ALL registries are searched.
|
|
911
|
+
|
|
912
|
+
By default, the dynamic component is registered under the name `"dynamic"`. In case of a conflict, you can change the name used for the dynamic components by defining the [`COMPONENTS.dynamic_component_name` setting](#dynamic_component_name).
|
|
913
|
+
|
|
914
|
+
If you need to use the dynamic components in Python, you can also import it from `django_components`:
|
|
915
|
+
```py
|
|
916
|
+
from django_components import DynamicComponent
|
|
917
|
+
|
|
918
|
+
comp = SimpleTableComp if is_readonly else TableComp
|
|
919
|
+
|
|
920
|
+
output = DynamicComponent.render(
|
|
921
|
+
kwargs={
|
|
922
|
+
"is": comp,
|
|
923
|
+
# Other kwargs...
|
|
924
|
+
},
|
|
925
|
+
# args: [...],
|
|
926
|
+
# slots: {...},
|
|
927
|
+
)
|
|
928
|
+
```
|
|
929
|
+
|
|
877
930
|
## Registering components
|
|
878
931
|
|
|
879
932
|
In previous examples you could repeatedly see us using `@register()` to "register"
|
|
@@ -1583,7 +1636,7 @@ This means that you can use `self.input` inside:
|
|
|
1583
1636
|
- `get_template_name`
|
|
1584
1637
|
- `get_template_string`
|
|
1585
1638
|
|
|
1586
|
-
`self.input` is defined only for the duration of `Component.render`, and
|
|
1639
|
+
`self.input` is defined only for the duration of `Component.render`, and raises `RuntimeError` when called outside of this.
|
|
1587
1640
|
|
|
1588
1641
|
`self.input` has the same fields as the input to `Component.render`:
|
|
1589
1642
|
|
|
@@ -2816,6 +2869,7 @@ Here's overview of all available settings and their defaults:
|
|
|
2816
2869
|
COMPONENTS = {
|
|
2817
2870
|
"autodiscover": True,
|
|
2818
2871
|
"context_behavior": "django", # "django" | "isolated"
|
|
2872
|
+
"dynamic_component_name": "dynamic",
|
|
2819
2873
|
"libraries": [], # ["mysite.components.forms", ...]
|
|
2820
2874
|
"multiline_tags": True,
|
|
2821
2875
|
"reload_on_template_change": False,
|
|
@@ -2876,6 +2930,16 @@ COMPONENTS = {
|
|
|
2876
2930
|
}
|
|
2877
2931
|
```
|
|
2878
2932
|
|
|
2933
|
+
### `dynamic_component_name`
|
|
2934
|
+
|
|
2935
|
+
By default, the dynamic component is registered under the name `"dynamic"`. In case of a conflict, use this setting to change the name used for the dynamic components.
|
|
2936
|
+
|
|
2937
|
+
```python
|
|
2938
|
+
COMPONENTS = {
|
|
2939
|
+
"dynamic_component_name": "new_dynamic",
|
|
2940
|
+
}
|
|
2941
|
+
```
|
|
2942
|
+
|
|
2879
2943
|
### `multiline_tags` - Enable/Disable multiline support
|
|
2880
2944
|
|
|
2881
2945
|
If `True`, template tags can span multiple lines. Default: `True`
|
|
@@ -44,8 +44,9 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
44
44
|
- [Using single-file components](#using-single-file-components)
|
|
45
45
|
- [Use components in templates](#use-components-in-templates)
|
|
46
46
|
- [Use components outside of templates](#use-components-outside-of-templates)
|
|
47
|
-
- [Registering components](#registering-components)
|
|
48
47
|
- [Use components as views](#use-components-as-views)
|
|
48
|
+
- [Pre-defined components](#pre-defined-components)
|
|
49
|
+
- [Registering components](#registering-components)
|
|
49
50
|
- [Autodiscovery](#autodiscovery)
|
|
50
51
|
- [Using slots in templates](#using-slots-in-templates)
|
|
51
52
|
- [Accessing data passed to the component](#accessing-data-passed-to-the-component)
|
|
@@ -67,6 +68,10 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
67
68
|
|
|
68
69
|
## Release notes
|
|
69
70
|
|
|
71
|
+
**Version 0.95**
|
|
72
|
+
- Added support for dynamic components, where the component name is passed as a variable. (See [Dynamic components](#dynamic-components))
|
|
73
|
+
- Changed `Component.input` to raise `RuntimeError` if accessed outside of render context. Previously it returned `None` if unset.
|
|
74
|
+
|
|
70
75
|
**Version 0.94**
|
|
71
76
|
- django_components now automatically configures Django to support multi-line tags. (See [Multi-line tags](#multi-line-tags))
|
|
72
77
|
- New setting `reload_on_template_change`. Set this to `True` to reload the dev server on changes to component template files. (See [Reload dev server on component file changes](#reload-dev-server-on-component-file-changes))
|
|
@@ -850,6 +855,54 @@ class MyComponent(Component):
|
|
|
850
855
|
do_something_extra(request, *args, **kwargs)
|
|
851
856
|
```
|
|
852
857
|
|
|
858
|
+
## Pre-defined components
|
|
859
|
+
|
|
860
|
+
### Dynamic components
|
|
861
|
+
|
|
862
|
+
If you are writing something like a form component, you may design it such that users
|
|
863
|
+
give you the component names, and your component renders it.
|
|
864
|
+
|
|
865
|
+
While you can handle this with a series of if / else statements, this is not an extensible solution.
|
|
866
|
+
|
|
867
|
+
Instead, you can use **dynamic components**. Dynamic components are used in place of normal components.
|
|
868
|
+
|
|
869
|
+
```django
|
|
870
|
+
{% load component_tags %}
|
|
871
|
+
{% component "dynamic" is=component_name title="Cat Museum" %}
|
|
872
|
+
{% fill "content" %}
|
|
873
|
+
HELLO_FROM_SLOT_1
|
|
874
|
+
{% endfill %}
|
|
875
|
+
{% fill "sidebar" %}
|
|
876
|
+
HELLO_FROM_SLOT_2
|
|
877
|
+
{% endfill %}
|
|
878
|
+
{% endcomponent %}
|
|
879
|
+
```
|
|
880
|
+
|
|
881
|
+
These behave same way as regular components. You pass it the same args, kwargs, and slots as you would
|
|
882
|
+
to the component that you want to render.
|
|
883
|
+
|
|
884
|
+
The only exception is that also you supply 1-2 additional inputs:
|
|
885
|
+
- `is` - Required - The component name or a component class to render
|
|
886
|
+
- `registry` - Optional - The `ComponentRegistry` that will be searched if `is` is a component name. If omitted, ALL registries are searched.
|
|
887
|
+
|
|
888
|
+
By default, the dynamic component is registered under the name `"dynamic"`. In case of a conflict, you can change the name used for the dynamic components by defining the [`COMPONENTS.dynamic_component_name` setting](#dynamic_component_name).
|
|
889
|
+
|
|
890
|
+
If you need to use the dynamic components in Python, you can also import it from `django_components`:
|
|
891
|
+
```py
|
|
892
|
+
from django_components import DynamicComponent
|
|
893
|
+
|
|
894
|
+
comp = SimpleTableComp if is_readonly else TableComp
|
|
895
|
+
|
|
896
|
+
output = DynamicComponent.render(
|
|
897
|
+
kwargs={
|
|
898
|
+
"is": comp,
|
|
899
|
+
# Other kwargs...
|
|
900
|
+
},
|
|
901
|
+
# args: [...],
|
|
902
|
+
# slots: {...},
|
|
903
|
+
)
|
|
904
|
+
```
|
|
905
|
+
|
|
853
906
|
## Registering components
|
|
854
907
|
|
|
855
908
|
In previous examples you could repeatedly see us using `@register()` to "register"
|
|
@@ -1559,7 +1612,7 @@ This means that you can use `self.input` inside:
|
|
|
1559
1612
|
- `get_template_name`
|
|
1560
1613
|
- `get_template_string`
|
|
1561
1614
|
|
|
1562
|
-
`self.input` is defined only for the duration of `Component.render`, and
|
|
1615
|
+
`self.input` is defined only for the duration of `Component.render`, and raises `RuntimeError` when called outside of this.
|
|
1563
1616
|
|
|
1564
1617
|
`self.input` has the same fields as the input to `Component.render`:
|
|
1565
1618
|
|
|
@@ -2792,6 +2845,7 @@ Here's overview of all available settings and their defaults:
|
|
|
2792
2845
|
COMPONENTS = {
|
|
2793
2846
|
"autodiscover": True,
|
|
2794
2847
|
"context_behavior": "django", # "django" | "isolated"
|
|
2848
|
+
"dynamic_component_name": "dynamic",
|
|
2795
2849
|
"libraries": [], # ["mysite.components.forms", ...]
|
|
2796
2850
|
"multiline_tags": True,
|
|
2797
2851
|
"reload_on_template_change": False,
|
|
@@ -2852,6 +2906,16 @@ COMPONENTS = {
|
|
|
2852
2906
|
}
|
|
2853
2907
|
```
|
|
2854
2908
|
|
|
2909
|
+
### `dynamic_component_name`
|
|
2910
|
+
|
|
2911
|
+
By default, the dynamic component is registered under the name `"dynamic"`. In case of a conflict, use this setting to change the name used for the dynamic components.
|
|
2912
|
+
|
|
2913
|
+
```python
|
|
2914
|
+
COMPONENTS = {
|
|
2915
|
+
"dynamic_component_name": "new_dynamic",
|
|
2916
|
+
}
|
|
2917
|
+
```
|
|
2918
|
+
|
|
2855
2919
|
### `multiline_tags` - Enable/Disable multiline support
|
|
2856
2920
|
|
|
2857
2921
|
If `True`, template tags can span multiple lines. Default: `True`
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "django_components"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.95"
|
|
8
8
|
requires-python = ">=3.8, <4.0"
|
|
9
9
|
description = "A way to create simple reusable template components in Django."
|
|
10
10
|
keywords = ["django", "components", "css", "js", "html"]
|
|
@@ -22,6 +22,7 @@ from django_components.component_registry import (
|
|
|
22
22
|
register as register,
|
|
23
23
|
registry as registry,
|
|
24
24
|
)
|
|
25
|
+
from django_components.components import DynamicComponent as DynamicComponent
|
|
25
26
|
from django_components.library import TagProtectedError as TagProtectedError
|
|
26
27
|
from django_components.slots import (
|
|
27
28
|
SlotContent as SlotContent,
|
|
@@ -99,7 +99,11 @@ class AppSettings:
|
|
|
99
99
|
return self.settings.get("autodiscover", True)
|
|
100
100
|
|
|
101
101
|
@property
|
|
102
|
-
def
|
|
102
|
+
def DYNAMIC_COMPONENT_NAME(self) -> str:
|
|
103
|
+
return self.settings.get("dynamic_component_name", "dynamic")
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def LIBRARIES(self) -> List[str]:
|
|
103
107
|
return self.settings.get("libraries", [])
|
|
104
108
|
|
|
105
109
|
@property
|
|
@@ -11,6 +11,8 @@ class ComponentsConfig(AppConfig):
|
|
|
11
11
|
def ready(self) -> None:
|
|
12
12
|
from django_components.app_settings import app_settings
|
|
13
13
|
from django_components.autodiscover import autodiscover, get_dirs, import_libraries, search_dirs
|
|
14
|
+
from django_components.component_registry import registry
|
|
15
|
+
from django_components.components.dynamic import DynamicComponent
|
|
14
16
|
from django_components.utils import watch_files_for_autoreload
|
|
15
17
|
|
|
16
18
|
# Import modules set in `COMPONENTS.libraries` setting
|
|
@@ -27,19 +29,22 @@ class ComponentsConfig(AppConfig):
|
|
|
27
29
|
component_filepaths = search_dirs(dirs, "**/*")
|
|
28
30
|
watch_files_for_autoreload(component_filepaths)
|
|
29
31
|
|
|
32
|
+
# Allow tags to span multiple lines. This makes it easier to work with
|
|
33
|
+
# components inside Django templates, allowing us syntax like:
|
|
34
|
+
# ```html
|
|
35
|
+
# {% component "icon"
|
|
36
|
+
# icon='outline_chevron_down'
|
|
37
|
+
# size=16
|
|
38
|
+
# color="text-gray-400"
|
|
39
|
+
# attrs:class="ml-2"
|
|
40
|
+
# %}{% endcomponent %}
|
|
41
|
+
# ```
|
|
42
|
+
#
|
|
43
|
+
# See https://stackoverflow.com/a/54206609/9788634
|
|
30
44
|
if app_settings.MULTILINE_TAGS:
|
|
31
|
-
# Allow tags to span multiple lines. This makes it easier to work with
|
|
32
|
-
# components inside Django templates, allowing us syntax like:
|
|
33
|
-
# ```html
|
|
34
|
-
# {% component "icon"
|
|
35
|
-
# icon='outline_chevron_down'
|
|
36
|
-
# size=16
|
|
37
|
-
# color="text-gray-400"
|
|
38
|
-
# attrs:class="ml-2"
|
|
39
|
-
# %}{% endcomponent %}
|
|
40
|
-
# ```
|
|
41
|
-
#
|
|
42
|
-
# See https://stackoverflow.com/a/54206609/9788634
|
|
43
45
|
from django.template import base
|
|
44
46
|
|
|
45
47
|
base.tag_re = re.compile(base.tag_re.pattern, re.DOTALL)
|
|
48
|
+
|
|
49
|
+
# Register the dynamic component under the name as given in settings
|
|
50
|
+
registry.register(app_settings.DYNAMIC_COMPONENT_NAME, DynamicComponent)
|
|
@@ -205,14 +205,17 @@ class Component(Generic[ArgsType, KwargsType, DataType, SlotsType], metaclass=Co
|
|
|
205
205
|
return self.registered_name or self.__class__.__name__
|
|
206
206
|
|
|
207
207
|
@property
|
|
208
|
-
def input(self) ->
|
|
208
|
+
def input(self) -> RenderInput[ArgsType, KwargsType, SlotsType]:
|
|
209
209
|
"""
|
|
210
210
|
Input holds the data (like arg, kwargs, slots) that were passsed to
|
|
211
211
|
the current execution of the `render` method.
|
|
212
212
|
"""
|
|
213
|
+
if not len(self._render_stack):
|
|
214
|
+
raise RuntimeError(f"{self.name}: Tried to access Component input while outside of rendering execution")
|
|
215
|
+
|
|
213
216
|
# NOTE: Input is managed as a stack, so if `render` is called within another `render`,
|
|
214
217
|
# the propertes below will return only the inner-most state.
|
|
215
|
-
return self._render_stack[-1]
|
|
218
|
+
return self._render_stack[-1]
|
|
216
219
|
|
|
217
220
|
def get_context_data(self, *args: Any, **kwargs: Any) -> DataType:
|
|
218
221
|
return cast(DataType, {})
|
|
@@ -526,6 +529,8 @@ class Component(Generic[ArgsType, KwargsType, DataType, SlotsType], metaclass=Co
|
|
|
526
529
|
component_name=self.name,
|
|
527
530
|
context_data=slot_context_data,
|
|
528
531
|
fill_content=fill_content,
|
|
532
|
+
# Dynamic component has a special mark do it doesn't raise certain errors
|
|
533
|
+
is_dynamic_component=getattr(self, "_is_dynamic_component", False),
|
|
529
534
|
)
|
|
530
535
|
|
|
531
536
|
# Available slot fills - this is internal to us
|
{django_components-0.94 → django_components-0.95}/src/django_components/component_registry.py
RENAMED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import TYPE_CHECKING, Callable, Dict, NamedTuple, Optional, Set, Type, TypeVar, Union
|
|
1
|
+
from typing import TYPE_CHECKING, Callable, Dict, List, NamedTuple, Optional, Set, Type, TypeVar, Union
|
|
2
2
|
|
|
3
3
|
from django.template import Library
|
|
4
4
|
|
|
@@ -44,6 +44,12 @@ class InternalRegistrySettings(NamedTuple):
|
|
|
44
44
|
TAG_FORMATTER: Union["TagFormatterABC", str]
|
|
45
45
|
|
|
46
46
|
|
|
47
|
+
# We keep track of all registries that exist so that, when users want to
|
|
48
|
+
# dynamically resolve component name to component class, they would be able
|
|
49
|
+
# to search across all registries.
|
|
50
|
+
all_registries: List["ComponentRegistry"] = []
|
|
51
|
+
|
|
52
|
+
|
|
47
53
|
class ComponentRegistry:
|
|
48
54
|
"""
|
|
49
55
|
Manages which components can be used in the template tags.
|
|
@@ -88,6 +94,8 @@ class ComponentRegistry:
|
|
|
88
94
|
self._settings_input = settings
|
|
89
95
|
self._settings: Optional[Callable[[], InternalRegistrySettings]] = None
|
|
90
96
|
|
|
97
|
+
all_registries.append(self)
|
|
98
|
+
|
|
91
99
|
@property
|
|
92
100
|
def library(self) -> Library:
|
|
93
101
|
"""
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from typing import Any, Dict, Optional, Type, Union, cast
|
|
3
|
+
|
|
4
|
+
from django_components import Component, ComponentRegistry, NotRegistered, types
|
|
5
|
+
from django_components.component_registry import all_registries
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class DynamicComponent(Component):
|
|
9
|
+
"""
|
|
10
|
+
Dynamic component - This component takes inputs and renders the outputs depending on the
|
|
11
|
+
`is` and `registry` arguments.
|
|
12
|
+
|
|
13
|
+
- `is` - required - The component class or registered name of the component that will be
|
|
14
|
+
rendered in this place.
|
|
15
|
+
|
|
16
|
+
- `registry` - optional - Specify the registry to search for the registered name. If omitted,
|
|
17
|
+
all registries are searched.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
_is_dynamic_component = True
|
|
21
|
+
|
|
22
|
+
def get_context_data(
|
|
23
|
+
self,
|
|
24
|
+
*args: Any,
|
|
25
|
+
registry: Optional[ComponentRegistry] = None,
|
|
26
|
+
**kwargs: Any,
|
|
27
|
+
) -> Dict:
|
|
28
|
+
# NOTE: We have to access `is` via kwargs, because it's a special keyword in Python
|
|
29
|
+
comp_name_or_class: Union[str, Type[Component]] = kwargs.pop("is", None)
|
|
30
|
+
if not comp_name_or_class:
|
|
31
|
+
raise TypeError(f"Component '{self.name}' is missing a required argument 'is'")
|
|
32
|
+
|
|
33
|
+
comp_class = self._resolve_component(comp_name_or_class, registry)
|
|
34
|
+
|
|
35
|
+
comp = comp_class(
|
|
36
|
+
registered_name=self.registered_name,
|
|
37
|
+
component_id=self.component_id,
|
|
38
|
+
outer_context=self.outer_context,
|
|
39
|
+
fill_content=self.fill_content,
|
|
40
|
+
registry=self.registry,
|
|
41
|
+
)
|
|
42
|
+
output = comp.render(
|
|
43
|
+
context=self.input.context,
|
|
44
|
+
args=args,
|
|
45
|
+
kwargs=kwargs,
|
|
46
|
+
escape_slots_content=self.input.escape_slots_content,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
"output": output,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
template: types.django_html = """
|
|
54
|
+
{{ output }}
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
def _resolve_component(
|
|
58
|
+
self,
|
|
59
|
+
comp_name_or_class: Union[str, Type[Component], Any],
|
|
60
|
+
registry: Optional[ComponentRegistry] = None,
|
|
61
|
+
) -> Type[Component]:
|
|
62
|
+
component_cls: Optional[Type[Component]] = None
|
|
63
|
+
|
|
64
|
+
if not isinstance(comp_name_or_class, str):
|
|
65
|
+
# NOTE: When Django template is resolving the variable that refers to the
|
|
66
|
+
# component class, it may see that it's callable and evaluate it. Hence, we need
|
|
67
|
+
# get check if we've got class or instance.
|
|
68
|
+
if inspect.isclass(comp_name_or_class):
|
|
69
|
+
component_cls = comp_name_or_class
|
|
70
|
+
else:
|
|
71
|
+
component_cls = cast(Type[Component], comp_name_or_class.__class__)
|
|
72
|
+
|
|
73
|
+
else:
|
|
74
|
+
if registry:
|
|
75
|
+
component_cls = registry.get(comp_name_or_class)
|
|
76
|
+
else:
|
|
77
|
+
# Search all registries for the first match
|
|
78
|
+
for reg in all_registries:
|
|
79
|
+
try:
|
|
80
|
+
component_cls = reg.get(comp_name_or_class)
|
|
81
|
+
break
|
|
82
|
+
except NotRegistered:
|
|
83
|
+
continue
|
|
84
|
+
|
|
85
|
+
# Raise if none found
|
|
86
|
+
if not component_cls:
|
|
87
|
+
raise NotRegistered(f"The component '{comp_name_or_class}' was not found")
|
|
88
|
+
|
|
89
|
+
return component_cls
|
|
@@ -473,6 +473,7 @@ def resolve_slots(
|
|
|
473
473
|
component_name: Optional[str],
|
|
474
474
|
context_data: Mapping[str, Any],
|
|
475
475
|
fill_content: Dict[SlotName, FillContent],
|
|
476
|
+
is_dynamic_component: bool = False,
|
|
476
477
|
) -> Tuple[Dict[SlotId, Slot], Dict[SlotId, SlotFill]]:
|
|
477
478
|
"""
|
|
478
479
|
Search the template for all SlotNodes, and associate the slots
|
|
@@ -546,10 +547,14 @@ def resolve_slots(
|
|
|
546
547
|
component_name=component_name,
|
|
547
548
|
slots=slots,
|
|
548
549
|
slot_fills=slot_fills,
|
|
550
|
+
is_dynamic_component=is_dynamic_component,
|
|
549
551
|
)
|
|
550
552
|
|
|
551
553
|
# 4. Detect any errors with slots/fills
|
|
552
|
-
|
|
554
|
+
# NOTE: We ignore errors for the dynamic component, as the underlying component
|
|
555
|
+
# will deal with it
|
|
556
|
+
if not is_dynamic_component:
|
|
557
|
+
_report_slot_errors(slots, slot_fills, component_name)
|
|
553
558
|
|
|
554
559
|
# 5. Find roots of the slot relationships
|
|
555
560
|
top_level_slot_ids: List[SlotId] = []
|
|
@@ -598,6 +603,7 @@ def _resolve_default_slot(
|
|
|
598
603
|
component_name: Optional[str],
|
|
599
604
|
slots: Dict[SlotId, Slot],
|
|
600
605
|
slot_fills: Dict[SlotName, SlotFill],
|
|
606
|
+
is_dynamic_component: bool,
|
|
601
607
|
) -> Dict[SlotName, SlotFill]:
|
|
602
608
|
"""Figure out which slot the default fill refers to, and perform checks."""
|
|
603
609
|
named_fills = slot_fills.copy()
|
|
@@ -637,7 +643,7 @@ def _resolve_default_slot(
|
|
|
637
643
|
|
|
638
644
|
# Check: Only component templates that include a 'default' slot
|
|
639
645
|
# can be invoked with implicit filling.
|
|
640
|
-
if default_fill and not default_slot_encountered:
|
|
646
|
+
if default_fill and not default_slot_encountered and not is_dynamic_component:
|
|
641
647
|
raise TemplateSyntaxError(
|
|
642
648
|
f"Component '{component_name}' passed default fill content '{default_fill.name}'"
|
|
643
649
|
f"(i.e. without explicit 'fill' tag), "
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.95
|
|
4
4
|
Summary: A way to create simple reusable template components in Django.
|
|
5
5
|
Author-email: Emil Stenström <emil@emilstenstrom.se>
|
|
6
6
|
License: MIT
|
|
@@ -68,8 +68,9 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
68
68
|
- [Using single-file components](#using-single-file-components)
|
|
69
69
|
- [Use components in templates](#use-components-in-templates)
|
|
70
70
|
- [Use components outside of templates](#use-components-outside-of-templates)
|
|
71
|
-
- [Registering components](#registering-components)
|
|
72
71
|
- [Use components as views](#use-components-as-views)
|
|
72
|
+
- [Pre-defined components](#pre-defined-components)
|
|
73
|
+
- [Registering components](#registering-components)
|
|
73
74
|
- [Autodiscovery](#autodiscovery)
|
|
74
75
|
- [Using slots in templates](#using-slots-in-templates)
|
|
75
76
|
- [Accessing data passed to the component](#accessing-data-passed-to-the-component)
|
|
@@ -91,6 +92,10 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
91
92
|
|
|
92
93
|
## Release notes
|
|
93
94
|
|
|
95
|
+
**Version 0.95**
|
|
96
|
+
- Added support for dynamic components, where the component name is passed as a variable. (See [Dynamic components](#dynamic-components))
|
|
97
|
+
- Changed `Component.input` to raise `RuntimeError` if accessed outside of render context. Previously it returned `None` if unset.
|
|
98
|
+
|
|
94
99
|
**Version 0.94**
|
|
95
100
|
- django_components now automatically configures Django to support multi-line tags. (See [Multi-line tags](#multi-line-tags))
|
|
96
101
|
- New setting `reload_on_template_change`. Set this to `True` to reload the dev server on changes to component template files. (See [Reload dev server on component file changes](#reload-dev-server-on-component-file-changes))
|
|
@@ -874,6 +879,54 @@ class MyComponent(Component):
|
|
|
874
879
|
do_something_extra(request, *args, **kwargs)
|
|
875
880
|
```
|
|
876
881
|
|
|
882
|
+
## Pre-defined components
|
|
883
|
+
|
|
884
|
+
### Dynamic components
|
|
885
|
+
|
|
886
|
+
If you are writing something like a form component, you may design it such that users
|
|
887
|
+
give you the component names, and your component renders it.
|
|
888
|
+
|
|
889
|
+
While you can handle this with a series of if / else statements, this is not an extensible solution.
|
|
890
|
+
|
|
891
|
+
Instead, you can use **dynamic components**. Dynamic components are used in place of normal components.
|
|
892
|
+
|
|
893
|
+
```django
|
|
894
|
+
{% load component_tags %}
|
|
895
|
+
{% component "dynamic" is=component_name title="Cat Museum" %}
|
|
896
|
+
{% fill "content" %}
|
|
897
|
+
HELLO_FROM_SLOT_1
|
|
898
|
+
{% endfill %}
|
|
899
|
+
{% fill "sidebar" %}
|
|
900
|
+
HELLO_FROM_SLOT_2
|
|
901
|
+
{% endfill %}
|
|
902
|
+
{% endcomponent %}
|
|
903
|
+
```
|
|
904
|
+
|
|
905
|
+
These behave same way as regular components. You pass it the same args, kwargs, and slots as you would
|
|
906
|
+
to the component that you want to render.
|
|
907
|
+
|
|
908
|
+
The only exception is that also you supply 1-2 additional inputs:
|
|
909
|
+
- `is` - Required - The component name or a component class to render
|
|
910
|
+
- `registry` - Optional - The `ComponentRegistry` that will be searched if `is` is a component name. If omitted, ALL registries are searched.
|
|
911
|
+
|
|
912
|
+
By default, the dynamic component is registered under the name `"dynamic"`. In case of a conflict, you can change the name used for the dynamic components by defining the [`COMPONENTS.dynamic_component_name` setting](#dynamic_component_name).
|
|
913
|
+
|
|
914
|
+
If you need to use the dynamic components in Python, you can also import it from `django_components`:
|
|
915
|
+
```py
|
|
916
|
+
from django_components import DynamicComponent
|
|
917
|
+
|
|
918
|
+
comp = SimpleTableComp if is_readonly else TableComp
|
|
919
|
+
|
|
920
|
+
output = DynamicComponent.render(
|
|
921
|
+
kwargs={
|
|
922
|
+
"is": comp,
|
|
923
|
+
# Other kwargs...
|
|
924
|
+
},
|
|
925
|
+
# args: [...],
|
|
926
|
+
# slots: {...},
|
|
927
|
+
)
|
|
928
|
+
```
|
|
929
|
+
|
|
877
930
|
## Registering components
|
|
878
931
|
|
|
879
932
|
In previous examples you could repeatedly see us using `@register()` to "register"
|
|
@@ -1583,7 +1636,7 @@ This means that you can use `self.input` inside:
|
|
|
1583
1636
|
- `get_template_name`
|
|
1584
1637
|
- `get_template_string`
|
|
1585
1638
|
|
|
1586
|
-
`self.input` is defined only for the duration of `Component.render`, and
|
|
1639
|
+
`self.input` is defined only for the duration of `Component.render`, and raises `RuntimeError` when called outside of this.
|
|
1587
1640
|
|
|
1588
1641
|
`self.input` has the same fields as the input to `Component.render`:
|
|
1589
1642
|
|
|
@@ -2816,6 +2869,7 @@ Here's overview of all available settings and their defaults:
|
|
|
2816
2869
|
COMPONENTS = {
|
|
2817
2870
|
"autodiscover": True,
|
|
2818
2871
|
"context_behavior": "django", # "django" | "isolated"
|
|
2872
|
+
"dynamic_component_name": "dynamic",
|
|
2819
2873
|
"libraries": [], # ["mysite.components.forms", ...]
|
|
2820
2874
|
"multiline_tags": True,
|
|
2821
2875
|
"reload_on_template_change": False,
|
|
@@ -2876,6 +2930,16 @@ COMPONENTS = {
|
|
|
2876
2930
|
}
|
|
2877
2931
|
```
|
|
2878
2932
|
|
|
2933
|
+
### `dynamic_component_name`
|
|
2934
|
+
|
|
2935
|
+
By default, the dynamic component is registered under the name `"dynamic"`. In case of a conflict, use this setting to change the name used for the dynamic components.
|
|
2936
|
+
|
|
2937
|
+
```python
|
|
2938
|
+
COMPONENTS = {
|
|
2939
|
+
"dynamic_component_name": "new_dynamic",
|
|
2940
|
+
}
|
|
2941
|
+
```
|
|
2942
|
+
|
|
2879
2943
|
### `multiline_tags` - Enable/Disable multiline support
|
|
2880
2944
|
|
|
2881
2945
|
If `True`, template tags can span multiple lines. Default: `True`
|
{django_components-0.94 → django_components-0.95}/src/django_components.egg-info/SOURCES.txt
RENAMED
|
@@ -28,6 +28,8 @@ src/django_components.egg-info/SOURCES.txt
|
|
|
28
28
|
src/django_components.egg-info/dependency_links.txt
|
|
29
29
|
src/django_components.egg-info/requires.txt
|
|
30
30
|
src/django_components.egg-info/top_level.txt
|
|
31
|
+
src/django_components/components/__init__.py
|
|
32
|
+
src/django_components/components/dynamic.py
|
|
31
33
|
src/django_components/management/__init__.py
|
|
32
34
|
src/django_components/management/commands/__init__.py
|
|
33
35
|
src/django_components/management/commands/startcomponent.py
|
|
@@ -2,12 +2,12 @@ import textwrap
|
|
|
2
2
|
|
|
3
3
|
from django.template import Context, Template, TemplateSyntaxError
|
|
4
4
|
|
|
5
|
-
from django_components import Component, NotRegistered, register, registry, types
|
|
5
|
+
from django_components import AlreadyRegistered, Component, NotRegistered, register, registry, types
|
|
6
6
|
|
|
7
7
|
from .django_test_setup import setup_test_config
|
|
8
8
|
from .testutils import BaseTestCase, parametrize_context_behavior
|
|
9
9
|
|
|
10
|
-
setup_test_config()
|
|
10
|
+
setup_test_config({"autodiscover": False})
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
class SlottedComponent(Component):
|
|
@@ -75,18 +75,6 @@ class ComponentTemplateTagTest(BaseTestCase):
|
|
|
75
75
|
rendered = template.render(Context({}))
|
|
76
76
|
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
|
|
77
77
|
|
|
78
|
-
@parametrize_context_behavior(["django", "isolated"])
|
|
79
|
-
def test_raises_on_no_registered_components(self):
|
|
80
|
-
# Note: No tag registered
|
|
81
|
-
|
|
82
|
-
simple_tag_template: types.django_html = """
|
|
83
|
-
{% load component_tags %}
|
|
84
|
-
{% component name="test" variable="variable" %}{% endcomponent %}
|
|
85
|
-
"""
|
|
86
|
-
|
|
87
|
-
with self.assertRaisesMessage(TemplateSyntaxError, "Invalid block tag on line 3: 'component'"):
|
|
88
|
-
Template(simple_tag_template)
|
|
89
|
-
|
|
90
78
|
@parametrize_context_behavior(["django", "isolated"])
|
|
91
79
|
def test_call_with_invalid_name(self):
|
|
92
80
|
registry.register(name="test_one", component=self.SimpleComponent)
|
|
@@ -199,6 +187,302 @@ class ComponentTemplateTagTest(BaseTestCase):
|
|
|
199
187
|
)
|
|
200
188
|
|
|
201
189
|
|
|
190
|
+
class DynamicComponentTemplateTagTest(BaseTestCase):
|
|
191
|
+
class SimpleComponent(Component):
|
|
192
|
+
template: types.django_html = """
|
|
193
|
+
Variable: <strong>{{ variable }}</strong>
|
|
194
|
+
"""
|
|
195
|
+
|
|
196
|
+
def get_context_data(self, variable, variable2="default"):
|
|
197
|
+
return {
|
|
198
|
+
"variable": variable,
|
|
199
|
+
"variable2": variable2,
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
class Media:
|
|
203
|
+
css = "style.css"
|
|
204
|
+
js = "script.js"
|
|
205
|
+
|
|
206
|
+
def setUp(self):
|
|
207
|
+
super().setUp()
|
|
208
|
+
|
|
209
|
+
# Run app installation so the `dynamic` component is defined
|
|
210
|
+
from django_components.apps import ComponentsConfig
|
|
211
|
+
|
|
212
|
+
ComponentsConfig.ready(None) # type: ignore[arg-type]
|
|
213
|
+
|
|
214
|
+
@parametrize_context_behavior(["django", "isolated"])
|
|
215
|
+
def test_basic(self):
|
|
216
|
+
registry.register(name="test", component=self.SimpleComponent)
|
|
217
|
+
|
|
218
|
+
simple_tag_template: types.django_html = """
|
|
219
|
+
{% load component_tags %}
|
|
220
|
+
{% component "dynamic" is="test" variable="variable" %}{% endcomponent %}
|
|
221
|
+
"""
|
|
222
|
+
|
|
223
|
+
template = Template(simple_tag_template)
|
|
224
|
+
rendered = template.render(Context({}))
|
|
225
|
+
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
|
|
226
|
+
|
|
227
|
+
@parametrize_context_behavior(["django", "isolated"])
|
|
228
|
+
def test_call_with_invalid_name(self):
|
|
229
|
+
registry.register(name="test", component=self.SimpleComponent)
|
|
230
|
+
|
|
231
|
+
simple_tag_template: types.django_html = """
|
|
232
|
+
{% load component_tags %}
|
|
233
|
+
{% component "dynamic" is="haber_der_baber" variable="variable" %}{% endcomponent %}
|
|
234
|
+
"""
|
|
235
|
+
|
|
236
|
+
template = Template(simple_tag_template)
|
|
237
|
+
with self.assertRaisesMessage(NotRegistered, "The component 'haber_der_baber' was not found"):
|
|
238
|
+
template.render(Context({}))
|
|
239
|
+
|
|
240
|
+
@parametrize_context_behavior(["django", "isolated"])
|
|
241
|
+
def test_component_called_with_variable_as_name(self):
|
|
242
|
+
registry.register(name="test", component=self.SimpleComponent)
|
|
243
|
+
|
|
244
|
+
simple_tag_template: types.django_html = """
|
|
245
|
+
{% load component_tags %}
|
|
246
|
+
{% with component_name="test" %}
|
|
247
|
+
{% component "dynamic" is=component_name variable="variable" %}{% endcomponent %}
|
|
248
|
+
{% endwith %}
|
|
249
|
+
"""
|
|
250
|
+
|
|
251
|
+
template = Template(simple_tag_template)
|
|
252
|
+
rendered = template.render(Context({}))
|
|
253
|
+
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
|
|
254
|
+
|
|
255
|
+
@parametrize_context_behavior(["django", "isolated"])
|
|
256
|
+
def test_component_called_with_variable_as_spread(self):
|
|
257
|
+
registry.register(name="test", component=self.SimpleComponent)
|
|
258
|
+
|
|
259
|
+
simple_tag_template: types.django_html = """
|
|
260
|
+
{% load component_tags %}
|
|
261
|
+
{% component "dynamic" ...props %}{% endcomponent %}
|
|
262
|
+
"""
|
|
263
|
+
|
|
264
|
+
template = Template(simple_tag_template)
|
|
265
|
+
rendered = template.render(
|
|
266
|
+
Context(
|
|
267
|
+
{
|
|
268
|
+
"props": {
|
|
269
|
+
"is": "test",
|
|
270
|
+
"variable": "variable",
|
|
271
|
+
},
|
|
272
|
+
}
|
|
273
|
+
)
|
|
274
|
+
)
|
|
275
|
+
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
|
|
276
|
+
|
|
277
|
+
@parametrize_context_behavior(["django", "isolated"])
|
|
278
|
+
def test_component_as_class(self):
|
|
279
|
+
registry.register(name="test", component=self.SimpleComponent)
|
|
280
|
+
|
|
281
|
+
simple_tag_template: types.django_html = """
|
|
282
|
+
{% load component_tags %}
|
|
283
|
+
{% component "dynamic" is=comp_cls variable="variable" %}{% endcomponent %}
|
|
284
|
+
"""
|
|
285
|
+
|
|
286
|
+
template = Template(simple_tag_template)
|
|
287
|
+
rendered = template.render(
|
|
288
|
+
Context(
|
|
289
|
+
{
|
|
290
|
+
"comp_cls": self.SimpleComponent,
|
|
291
|
+
}
|
|
292
|
+
)
|
|
293
|
+
)
|
|
294
|
+
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
|
|
295
|
+
|
|
296
|
+
@parametrize_context_behavior(
|
|
297
|
+
["django", "isolated"],
|
|
298
|
+
settings={
|
|
299
|
+
"COMPONENTS": {
|
|
300
|
+
"tag_formatter": "django_components.component_shorthand_formatter",
|
|
301
|
+
"autodiscover": False,
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
)
|
|
305
|
+
def test_shorthand_formatter(self):
|
|
306
|
+
from django_components.apps import ComponentsConfig
|
|
307
|
+
|
|
308
|
+
ComponentsConfig.ready(None) # type: ignore[arg-type]
|
|
309
|
+
|
|
310
|
+
registry.register(name="test", component=self.SimpleComponent)
|
|
311
|
+
|
|
312
|
+
simple_tag_template: types.django_html = """
|
|
313
|
+
{% load component_tags %}
|
|
314
|
+
{% dynamic is="test" variable="variable" %}{% enddynamic %}
|
|
315
|
+
"""
|
|
316
|
+
|
|
317
|
+
template = Template(simple_tag_template)
|
|
318
|
+
rendered = template.render(Context({}))
|
|
319
|
+
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
|
|
320
|
+
|
|
321
|
+
@parametrize_context_behavior(
|
|
322
|
+
["django", "isolated"],
|
|
323
|
+
settings={
|
|
324
|
+
"COMPONENTS": {
|
|
325
|
+
"dynamic_component_name": "uno_reverse",
|
|
326
|
+
"tag_formatter": "django_components.component_shorthand_formatter",
|
|
327
|
+
"autodiscover": False,
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
)
|
|
331
|
+
def test_component_name_is_configurable(self):
|
|
332
|
+
from django_components.apps import ComponentsConfig
|
|
333
|
+
|
|
334
|
+
ComponentsConfig.ready(None) # type: ignore[arg-type]
|
|
335
|
+
|
|
336
|
+
registry.register(name="test", component=self.SimpleComponent)
|
|
337
|
+
|
|
338
|
+
simple_tag_template: types.django_html = """
|
|
339
|
+
{% load component_tags %}
|
|
340
|
+
{% uno_reverse is="test" variable="variable" %}{% enduno_reverse %}
|
|
341
|
+
"""
|
|
342
|
+
|
|
343
|
+
template = Template(simple_tag_template)
|
|
344
|
+
rendered = template.render(Context({}))
|
|
345
|
+
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
|
|
346
|
+
|
|
347
|
+
@parametrize_context_behavior(["django", "isolated"])
|
|
348
|
+
def test_raises_already_registered_on_name_conflict(self):
|
|
349
|
+
with self.assertRaisesMessage(AlreadyRegistered, 'The component "dynamic" has already been registered'):
|
|
350
|
+
registry.register(name="dynamic", component=self.SimpleComponent)
|
|
351
|
+
|
|
352
|
+
@parametrize_context_behavior(["django", "isolated"])
|
|
353
|
+
def test_component_called_with_default_slot(self):
|
|
354
|
+
class SimpleSlottedComponent(Component):
|
|
355
|
+
template: types.django_html = """
|
|
356
|
+
{% load component_tags %}
|
|
357
|
+
Variable: <strong>{{ variable }}</strong>
|
|
358
|
+
Slot: {% slot "default" default / %}
|
|
359
|
+
"""
|
|
360
|
+
|
|
361
|
+
def get_context_data(self, variable, variable2="default"):
|
|
362
|
+
return {
|
|
363
|
+
"variable": variable,
|
|
364
|
+
"variable2": variable2,
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
registry.register(name="test", component=SimpleSlottedComponent)
|
|
368
|
+
|
|
369
|
+
simple_tag_template: types.django_html = """
|
|
370
|
+
{% load component_tags %}
|
|
371
|
+
{% with component_name="test" %}
|
|
372
|
+
{% component "dynamic" is=component_name variable="variable" %}
|
|
373
|
+
HELLO_FROM_SLOT
|
|
374
|
+
{% endcomponent %}
|
|
375
|
+
{% endwith %}
|
|
376
|
+
"""
|
|
377
|
+
|
|
378
|
+
template = Template(simple_tag_template)
|
|
379
|
+
rendered = template.render(Context({}))
|
|
380
|
+
self.assertHTMLEqual(
|
|
381
|
+
rendered,
|
|
382
|
+
"""
|
|
383
|
+
Variable: <strong>variable</strong>
|
|
384
|
+
Slot: HELLO_FROM_SLOT
|
|
385
|
+
""",
|
|
386
|
+
)
|
|
387
|
+
|
|
388
|
+
@parametrize_context_behavior(["django", "isolated"])
|
|
389
|
+
def test_component_called_with_named_slots(self):
|
|
390
|
+
class SimpleSlottedComponent(Component):
|
|
391
|
+
template: types.django_html = """
|
|
392
|
+
{% load component_tags %}
|
|
393
|
+
Variable: <strong>{{ variable }}</strong>
|
|
394
|
+
Slot 1: {% slot "default" default / %}
|
|
395
|
+
Slot 2: {% slot "two" / %}
|
|
396
|
+
"""
|
|
397
|
+
|
|
398
|
+
def get_context_data(self, variable, variable2="default"):
|
|
399
|
+
return {
|
|
400
|
+
"variable": variable,
|
|
401
|
+
"variable2": variable2,
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
registry.register(name="test", component=SimpleSlottedComponent)
|
|
405
|
+
|
|
406
|
+
simple_tag_template: types.django_html = """
|
|
407
|
+
{% load component_tags %}
|
|
408
|
+
{% with component_name="test" %}
|
|
409
|
+
{% component "dynamic" is=component_name variable="variable" %}
|
|
410
|
+
{% fill "default" %}
|
|
411
|
+
HELLO_FROM_SLOT_1
|
|
412
|
+
{% endfill %}
|
|
413
|
+
{% fill "two" %}
|
|
414
|
+
HELLO_FROM_SLOT_2
|
|
415
|
+
{% endfill %}
|
|
416
|
+
{% endcomponent %}
|
|
417
|
+
{% endwith %}
|
|
418
|
+
"""
|
|
419
|
+
|
|
420
|
+
template = Template(simple_tag_template)
|
|
421
|
+
rendered = template.render(Context({}))
|
|
422
|
+
self.assertHTMLEqual(
|
|
423
|
+
rendered,
|
|
424
|
+
"""
|
|
425
|
+
Variable: <strong>variable</strong>
|
|
426
|
+
Slot 1: HELLO_FROM_SLOT_1
|
|
427
|
+
Slot 2: HELLO_FROM_SLOT_2
|
|
428
|
+
""",
|
|
429
|
+
)
|
|
430
|
+
|
|
431
|
+
@parametrize_context_behavior(["django", "isolated"])
|
|
432
|
+
def test_raises_on_invalid_slots(self):
|
|
433
|
+
class SimpleSlottedComponent(Component):
|
|
434
|
+
template: types.django_html = """
|
|
435
|
+
{% load component_tags %}
|
|
436
|
+
Variable: <strong>{{ variable }}</strong>
|
|
437
|
+
Slot 1: {% slot "default" default / %}
|
|
438
|
+
Slot 2: {% slot "two" / %}
|
|
439
|
+
"""
|
|
440
|
+
|
|
441
|
+
def get_context_data(self, variable, variable2="default"):
|
|
442
|
+
return {
|
|
443
|
+
"variable": variable,
|
|
444
|
+
"variable2": variable2,
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
registry.register(name="test", component=SimpleSlottedComponent)
|
|
448
|
+
|
|
449
|
+
simple_tag_template: types.django_html = """
|
|
450
|
+
{% load component_tags %}
|
|
451
|
+
{% with component_name="test" %}
|
|
452
|
+
{% component "dynamic" is=component_name variable="variable" %}
|
|
453
|
+
{% fill "default" %}
|
|
454
|
+
HELLO_FROM_SLOT_1
|
|
455
|
+
{% endfill %}
|
|
456
|
+
{% fill "three" %}
|
|
457
|
+
HELLO_FROM_SLOT_2
|
|
458
|
+
{% endfill %}
|
|
459
|
+
{% endcomponent %}
|
|
460
|
+
{% endwith %}
|
|
461
|
+
"""
|
|
462
|
+
|
|
463
|
+
template = Template(simple_tag_template)
|
|
464
|
+
|
|
465
|
+
with self.assertRaisesMessage(
|
|
466
|
+
TemplateSyntaxError, "Component \\'dynamic\\' passed fill that refers to undefined slot: \\'three\\'"
|
|
467
|
+
):
|
|
468
|
+
template.render(Context({}))
|
|
469
|
+
|
|
470
|
+
@parametrize_context_behavior(["django", "isolated"])
|
|
471
|
+
def test_raises_on_invalid_args(self):
|
|
472
|
+
registry.register(name="test", component=self.SimpleComponent)
|
|
473
|
+
|
|
474
|
+
simple_tag_template: types.django_html = """
|
|
475
|
+
{% load component_tags %}
|
|
476
|
+
{% with component_name="test" %}
|
|
477
|
+
{% component "dynamic" is=component_name invalid_variable="variable" %}{% endcomponent %}
|
|
478
|
+
{% endwith %}
|
|
479
|
+
"""
|
|
480
|
+
|
|
481
|
+
template = Template(simple_tag_template)
|
|
482
|
+
with self.assertRaisesMessage(TypeError, "got an unexpected keyword argument \\'invalid_variable\\'"):
|
|
483
|
+
template.render(Context({}))
|
|
484
|
+
|
|
485
|
+
|
|
202
486
|
class MultiComponentTests(BaseTestCase):
|
|
203
487
|
def register_components(self):
|
|
204
488
|
registry.register("first_component", SlottedComponent)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.94 → django_components-0.95}/src/django_components/management/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.94 → django_components-0.95}/src/django_components/safer_staticfiles/apps.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.94 → django_components-0.95}/src/django_components/templatetags/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.94 → django_components-0.95}/src/django_components.egg-info/requires.txt
RENAMED
|
File without changes
|
{django_components-0.94 → django_components-0.95}/src/django_components.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|