django-components 0.36__tar.gz → 0.37__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.36 → django_components-0.37}/PKG-INFO +30 -34
- {django_components-0.36 → django_components-0.37}/README.md +29 -33
- {django_components-0.36 → django_components-0.37}/django_components/app_settings.py +6 -2
- {django_components-0.36 → django_components-0.37}/django_components/component.py +36 -13
- {django_components-0.36 → django_components-0.37}/django_components/component_registry.py +7 -2
- {django_components-0.36 → django_components-0.37}/django_components/management/commands/startcomponent.py +30 -8
- {django_components-0.36 → django_components-0.37}/django_components/middleware.py +22 -7
- {django_components-0.36 → django_components-0.37}/django_components/safer_staticfiles/apps.py +3 -1
- {django_components-0.36 → django_components-0.37}/django_components/template_loader.py +1 -1
- {django_components-0.36 → django_components-0.37}/django_components/templatetags/component_tags.py +157 -48
- {django_components-0.36 → django_components-0.37}/django_components.egg-info/PKG-INFO +30 -34
- {django_components-0.36 → django_components-0.37}/django_components.egg-info/SOURCES.txt +1 -1
- {django_components-0.36 → django_components-0.37}/pyproject.toml +2 -16
- django_components-0.37/setup.cfg +17 -0
- {django_components-0.36 → django_components-0.37}/setup.py +4 -2
- {django_components-0.36 → django_components-0.37}/tests/test_autodiscover.py +47 -10
- {django_components-0.36 → django_components-0.37}/tests/test_component.py +18 -10
- {django_components-0.36 → django_components-0.37}/tests/test_component_as_view.py +10 -4
- django_components-0.37/tests/test_context.py +512 -0
- {django_components-0.36 → django_components-0.37}/tests/test_dependency_rendering.py +70 -48
- {django_components-0.36 → django_components-0.37}/tests/test_registry.py +12 -4
- {django_components-0.36 → django_components-0.37}/tests/test_startcomponent_command.py +15 -5
- {django_components-0.36 → django_components-0.37}/tests/test_templatetags.py +232 -169
- {django_components-0.36 → django_components-0.37}/tests/testutils.py +9 -3
- django_components-0.36/django_components/management/commands/upgradecomponent.py +0 -65
- django_components-0.36/setup.cfg +0 -4
- django_components-0.36/tests/test_context.py +0 -386
- {django_components-0.36 → django_components-0.37}/LICENSE +0 -0
- {django_components-0.36 → django_components-0.37}/django_components/__init__.py +0 -0
- {django_components-0.36 → django_components-0.37}/django_components/apps.py +0 -0
- {django_components-0.36 → django_components-0.37}/django_components/management/__init__.py +0 -0
- {django_components-0.36 → django_components-0.37}/django_components/management/commands/__init__.py +0 -0
- {django_components-0.36 → django_components-0.37}/django_components/safer_staticfiles/__init__.py +0 -0
- {django_components-0.36 → django_components-0.37}/django_components/templatetags/__init__.py +0 -0
- {django_components-0.36 → django_components-0.37}/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.36 → django_components-0.37}/django_components.egg-info/requires.txt +0 -0
- {django_components-0.36 → django_components-0.37}/django_components.egg-info/top_level.txt +0 -0
- {django_components-0.36 → django_components-0.37}/tests/components/__init__.py +0 -0
- {django_components-0.36 → django_components-0.37}/tests/components/single_file.py +0 -0
- {django_components-0.36 → django_components-0.37}/tests/components/urls.py +0 -0
- {django_components-0.36 → django_components-0.37}/tests/test_settings.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.37
|
|
4
4
|
Summary: A way to create simple reusable template components in Django.
|
|
5
5
|
Home-page: https://github.com/EmilStenstrom/django-components/
|
|
6
6
|
Author: Emil Stenström
|
|
@@ -36,7 +36,7 @@ A way to create simple reusable template components in Django.
|
|
|
36
36
|
It lets you create "template components", that contains both the template, the Javascript and the CSS needed to generate the front end code you need for a modern app. Components look like this:
|
|
37
37
|
|
|
38
38
|
```htmldjango
|
|
39
|
-
{% component "calendar" date="2015-06-19" %}
|
|
39
|
+
{% component "calendar" date="2015-06-19" %}
|
|
40
40
|
```
|
|
41
41
|
|
|
42
42
|
And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
@@ -49,21 +49,17 @@ Read on to learn about the details!
|
|
|
49
49
|
|
|
50
50
|
## Release notes
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
*Version 0.34* adds components as views, which allows you to handle requests and render responses from within a component. See the [documentation](#components-as-views) for more details.
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
*Version 0.28* introduces 'implicit' slot filling and the `default` option for `slot` tags.
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
*Version 0.27* adds a second installable app: *django_components.safer_staticfiles*. It provides the same behavior as *django.contrib.staticfiles* but with extra security guarantees (more info below in Security Notes).
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
*Version 0.26* changes the syntax for `{% slot %}` tags. From now on, we separate defining a slot (`{% slot %}`) from filling a slot with content (`{% fill %}`). This means you will likely need to change a lot of slot tags to fill. We understand this is annoying, but it's the only way we can get support for nested slots that fill in other slots, which is a very nice feature to have access to. Hoping that this will feel worth it!
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
*Version 0.22* starts autoimporting all files inside components subdirectores, to simplify setup. An existing project might start to get AlreadyRegistered-errors because of this. To solve this, either remove your custom loading of components, or set "autodiscover": False in settings.COMPONENTS.
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
**Version 0.22** starts autoimporting all files inside components subdirectores, to simplify setup. An existing project might start to get AlreadyRegistered-errors because of this. To solve this, either remove your custom loading of components, or set "autodiscover": False in settings.COMPONENTS.
|
|
65
|
-
|
|
66
|
-
**Version 0.17** renames `Component.context` and `Component.template` to `get_context_data` and `get_template_name`. The old methods still work, but emit a deprecation warning. This change was done to sync naming with Django's class based views, and make using django-components more familiar to Django users. `Component.context` and `Component.template` will be removed when version 1.0 is released.
|
|
62
|
+
*Version 0.17* renames `Component.context` and `Component.template` to `get_context_data` and `get_template_name`. The old methods still work, but emit a deprecation warning. This change was done to sync naming with Django's class based views, and make using django-components more familiar to Django users. `Component.context` and `Component.template` will be removed when version 1.0 is released.
|
|
67
63
|
|
|
68
64
|
## Security notes 🚨
|
|
69
65
|
|
|
@@ -139,7 +135,7 @@ Modify STATICFILES_DIRS (or add it if you don't have it) so django can find your
|
|
|
139
135
|
```python
|
|
140
136
|
STATICFILES_DIRS = [
|
|
141
137
|
...,
|
|
142
|
-
BASE_DIR
|
|
138
|
+
os.path.join(BASE_DIR, "components"),
|
|
143
139
|
]
|
|
144
140
|
```
|
|
145
141
|
|
|
@@ -253,7 +249,7 @@ First load the `component_tags` tag library, then use the `component_[js/css]_de
|
|
|
253
249
|
{% component_css_dependencies %}
|
|
254
250
|
</head>
|
|
255
251
|
<body>
|
|
256
|
-
{% component "calendar" date="2015-06-19" %}
|
|
252
|
+
{% component "calendar" date="2015-06-19" %}
|
|
257
253
|
{% component_js_dependencies %}
|
|
258
254
|
</body>
|
|
259
255
|
<html>
|
|
@@ -330,7 +326,7 @@ This mechanism makes components more reusable and composable.
|
|
|
330
326
|
In the example below we introduce two block tags that work hand in hand to make this work. These are...
|
|
331
327
|
|
|
332
328
|
- `{% slot <name> %}`/`{% endslot %}`: Declares a new slot in the component template.
|
|
333
|
-
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `
|
|
329
|
+
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `component_block` tag pair.) Fills a declared slot with the specified content.
|
|
334
330
|
|
|
335
331
|
Let's update our calendar component to support more customization. We'll add `slot` tag pairs to its template, _calendar.html_.
|
|
336
332
|
|
|
@@ -348,9 +344,9 @@ Let's update our calendar component to support more customization. We'll add `sl
|
|
|
348
344
|
When using the component, you specify which slots you want to fill and where you want to use the defaults from the template. It looks like this:
|
|
349
345
|
|
|
350
346
|
```htmldjango
|
|
351
|
-
{%
|
|
347
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
352
348
|
{% fill "body" %}Can you believe it's already <span>{{ date }}</span>??{% endfill %}
|
|
353
|
-
{%
|
|
349
|
+
{% endcomponent_block %}
|
|
354
350
|
```
|
|
355
351
|
|
|
356
352
|
Since the header block is unspecified, it's taken from the base template. If you put this in a template, and pass in `date=2020-06-06`, this is what gets rendered:
|
|
@@ -368,7 +364,7 @@ Since the header block is unspecified, it's taken from the base template. If you
|
|
|
368
364
|
|
|
369
365
|
As you can see, component slots lets you write reusable containers that you fill in when you use a component. This makes for highly reusable components that can be used in different circumstances.
|
|
370
366
|
|
|
371
|
-
It can become tedious to use `fill` tags everywhere, especially when you're using a component that declares only one slot. To make things easier, `slot` tags can be marked with an optional keyword: `default`. When added to the end of the tag (as shown below), this option lets you pass filling content directly in the body of a `
|
|
367
|
+
It can become tedious to use `fill` tags everywhere, especially when you're using a component that declares only one slot. To make things easier, `slot` tags can be marked with an optional keyword: `default`. When added to the end of the tag (as shown below), this option lets you pass filling content directly in the body of a `component_block` tag pair – without using a `fill` tag. Choose carefully, though: a component template may contain at most one slot that is marked as `default`. The `default` option can be combined with other slot options, e.g. `required`.
|
|
372
368
|
|
|
373
369
|
Here's the same example as before, except with default slots and implicit filling.
|
|
374
370
|
|
|
@@ -388,9 +384,9 @@ The template:
|
|
|
388
384
|
Including the component (notice how the `fill` tag is omitted):
|
|
389
385
|
|
|
390
386
|
```htmldjango
|
|
391
|
-
{%
|
|
387
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
392
388
|
Can you believe it's already <span>{{ date }}</span>??
|
|
393
|
-
{%
|
|
389
|
+
{% endcomponent_block %}
|
|
394
390
|
```
|
|
395
391
|
|
|
396
392
|
The rendered result (exactly the same as before):
|
|
@@ -410,32 +406,32 @@ You may be tempted to combine implicit fills with explicit `fill` tags. This wil
|
|
|
410
406
|
|
|
411
407
|
```htmldjango
|
|
412
408
|
{# DON'T DO THIS #}
|
|
413
|
-
{%
|
|
409
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
414
410
|
{% fill "header" %}Totally new header!{% endfill %}
|
|
415
411
|
Can you believe it's already <span>{{ date }}</span>??
|
|
416
|
-
{%
|
|
412
|
+
{% endcomponent_block %}
|
|
417
413
|
```
|
|
418
414
|
|
|
419
415
|
By contrast, it is permitted to use `fill` tags in nested components, e.g.:
|
|
420
416
|
|
|
421
417
|
```htmldjango
|
|
422
|
-
{%
|
|
423
|
-
{%
|
|
418
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
419
|
+
{% component_block "beautiful-box" %}
|
|
424
420
|
{% fill "content" %} Can you believe it's already <span>{{ date }}</span>?? {% endfill %}
|
|
425
|
-
{%
|
|
426
|
-
{%
|
|
421
|
+
{% endcomponent_block %}
|
|
422
|
+
{% endcomponent_block %}
|
|
427
423
|
```
|
|
428
424
|
|
|
429
425
|
This is fine too:
|
|
430
426
|
|
|
431
427
|
```htmldjango
|
|
432
|
-
{%
|
|
428
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
433
429
|
{% fill "header" %}
|
|
434
|
-
{%
|
|
430
|
+
{% component_block "calendar-header" %}
|
|
435
431
|
Super Special Calendar Header
|
|
436
|
-
{%
|
|
432
|
+
{% endcomponent_block %}
|
|
437
433
|
{% endfill %}
|
|
438
|
-
{%
|
|
434
|
+
{% endcomponent_block %}
|
|
439
435
|
```
|
|
440
436
|
|
|
441
437
|
### Components as views
|
|
@@ -512,9 +508,9 @@ If you're planning on passing an HTML string, check Django's use of [`format_htm
|
|
|
512
508
|
Certain properties of a slot can be accessed from within a 'fill' context. They are provided as attributes on a user-defined alias of the targeted slot. For instance, let's say you're filling a slot called 'body'. To access properties of this slot, alias it using the 'as' keyword to a new name -- or keep the original name. With the new slot alias, you can call `<alias>.default` to insert the default content.
|
|
513
509
|
|
|
514
510
|
```htmldjango
|
|
515
|
-
{%
|
|
511
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
516
512
|
{% fill "body" as "body" %}{{ body.default }}. Have a great day!{% endfill %}
|
|
517
|
-
{%
|
|
513
|
+
{% endcomponent_block %}
|
|
518
514
|
```
|
|
519
515
|
|
|
520
516
|
Produces:
|
|
@@ -647,10 +643,10 @@ COMPONENTS = {
|
|
|
647
643
|
|
|
648
644
|
## Component context and scope
|
|
649
645
|
|
|
650
|
-
By default, components can access context variables from the parent template, just like templates that are included with the `{% include %}` tag. Just like with `{% include %}`, if you don't want the component template to have access to the parent context, add `only` to the end of the `{% component %}` tag):
|
|
646
|
+
By default, components can access context variables from the parent template, just like templates that are included with the `{% include %}` tag. Just like with `{% include %}`, if you don't want the component template to have access to the parent context, add `only` to the end of the `{% component %}` (or `{% component_block %}` tag):
|
|
651
647
|
|
|
652
648
|
```htmldjango
|
|
653
|
-
{% component "calendar" date="2015-06-19" only %}
|
|
649
|
+
{% component "calendar" date="2015-06-19" only %}
|
|
654
650
|
```
|
|
655
651
|
|
|
656
652
|
NOTE: `{% csrf_token %}` tags need access to the top-level context, and they will not function properly if they are rendered in a component that is called with the `only` modifier.
|
|
@@ -7,7 +7,7 @@ A way to create simple reusable template components in Django.
|
|
|
7
7
|
It lets you create "template components", that contains both the template, the Javascript and the CSS needed to generate the front end code you need for a modern app. Components look like this:
|
|
8
8
|
|
|
9
9
|
```htmldjango
|
|
10
|
-
{% component "calendar" date="2015-06-19" %}
|
|
10
|
+
{% component "calendar" date="2015-06-19" %}
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
@@ -20,21 +20,17 @@ Read on to learn about the details!
|
|
|
20
20
|
|
|
21
21
|
## Release notes
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
*Version 0.34* adds components as views, which allows you to handle requests and render responses from within a component. See the [documentation](#components-as-views) for more details.
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
*Version 0.28* introduces 'implicit' slot filling and the `default` option for `slot` tags.
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
*Version 0.27* adds a second installable app: *django_components.safer_staticfiles*. It provides the same behavior as *django.contrib.staticfiles* but with extra security guarantees (more info below in Security Notes).
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
*Version 0.26* changes the syntax for `{% slot %}` tags. From now on, we separate defining a slot (`{% slot %}`) from filling a slot with content (`{% fill %}`). This means you will likely need to change a lot of slot tags to fill. We understand this is annoying, but it's the only way we can get support for nested slots that fill in other slots, which is a very nice feature to have access to. Hoping that this will feel worth it!
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
*Version 0.22* starts autoimporting all files inside components subdirectores, to simplify setup. An existing project might start to get AlreadyRegistered-errors because of this. To solve this, either remove your custom loading of components, or set "autodiscover": False in settings.COMPONENTS.
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
**Version 0.22** starts autoimporting all files inside components subdirectores, to simplify setup. An existing project might start to get AlreadyRegistered-errors because of this. To solve this, either remove your custom loading of components, or set "autodiscover": False in settings.COMPONENTS.
|
|
36
|
-
|
|
37
|
-
**Version 0.17** renames `Component.context` and `Component.template` to `get_context_data` and `get_template_name`. The old methods still work, but emit a deprecation warning. This change was done to sync naming with Django's class based views, and make using django-components more familiar to Django users. `Component.context` and `Component.template` will be removed when version 1.0 is released.
|
|
33
|
+
*Version 0.17* renames `Component.context` and `Component.template` to `get_context_data` and `get_template_name`. The old methods still work, but emit a deprecation warning. This change was done to sync naming with Django's class based views, and make using django-components more familiar to Django users. `Component.context` and `Component.template` will be removed when version 1.0 is released.
|
|
38
34
|
|
|
39
35
|
## Security notes 🚨
|
|
40
36
|
|
|
@@ -110,7 +106,7 @@ Modify STATICFILES_DIRS (or add it if you don't have it) so django can find your
|
|
|
110
106
|
```python
|
|
111
107
|
STATICFILES_DIRS = [
|
|
112
108
|
...,
|
|
113
|
-
BASE_DIR
|
|
109
|
+
os.path.join(BASE_DIR, "components"),
|
|
114
110
|
]
|
|
115
111
|
```
|
|
116
112
|
|
|
@@ -224,7 +220,7 @@ First load the `component_tags` tag library, then use the `component_[js/css]_de
|
|
|
224
220
|
{% component_css_dependencies %}
|
|
225
221
|
</head>
|
|
226
222
|
<body>
|
|
227
|
-
{% component "calendar" date="2015-06-19" %}
|
|
223
|
+
{% component "calendar" date="2015-06-19" %}
|
|
228
224
|
{% component_js_dependencies %}
|
|
229
225
|
</body>
|
|
230
226
|
<html>
|
|
@@ -301,7 +297,7 @@ This mechanism makes components more reusable and composable.
|
|
|
301
297
|
In the example below we introduce two block tags that work hand in hand to make this work. These are...
|
|
302
298
|
|
|
303
299
|
- `{% slot <name> %}`/`{% endslot %}`: Declares a new slot in the component template.
|
|
304
|
-
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `
|
|
300
|
+
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `component_block` tag pair.) Fills a declared slot with the specified content.
|
|
305
301
|
|
|
306
302
|
Let's update our calendar component to support more customization. We'll add `slot` tag pairs to its template, _calendar.html_.
|
|
307
303
|
|
|
@@ -319,9 +315,9 @@ Let's update our calendar component to support more customization. We'll add `sl
|
|
|
319
315
|
When using the component, you specify which slots you want to fill and where you want to use the defaults from the template. It looks like this:
|
|
320
316
|
|
|
321
317
|
```htmldjango
|
|
322
|
-
{%
|
|
318
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
323
319
|
{% fill "body" %}Can you believe it's already <span>{{ date }}</span>??{% endfill %}
|
|
324
|
-
{%
|
|
320
|
+
{% endcomponent_block %}
|
|
325
321
|
```
|
|
326
322
|
|
|
327
323
|
Since the header block is unspecified, it's taken from the base template. If you put this in a template, and pass in `date=2020-06-06`, this is what gets rendered:
|
|
@@ -339,7 +335,7 @@ Since the header block is unspecified, it's taken from the base template. If you
|
|
|
339
335
|
|
|
340
336
|
As you can see, component slots lets you write reusable containers that you fill in when you use a component. This makes for highly reusable components that can be used in different circumstances.
|
|
341
337
|
|
|
342
|
-
It can become tedious to use `fill` tags everywhere, especially when you're using a component that declares only one slot. To make things easier, `slot` tags can be marked with an optional keyword: `default`. When added to the end of the tag (as shown below), this option lets you pass filling content directly in the body of a `
|
|
338
|
+
It can become tedious to use `fill` tags everywhere, especially when you're using a component that declares only one slot. To make things easier, `slot` tags can be marked with an optional keyword: `default`. When added to the end of the tag (as shown below), this option lets you pass filling content directly in the body of a `component_block` tag pair – without using a `fill` tag. Choose carefully, though: a component template may contain at most one slot that is marked as `default`. The `default` option can be combined with other slot options, e.g. `required`.
|
|
343
339
|
|
|
344
340
|
Here's the same example as before, except with default slots and implicit filling.
|
|
345
341
|
|
|
@@ -359,9 +355,9 @@ The template:
|
|
|
359
355
|
Including the component (notice how the `fill` tag is omitted):
|
|
360
356
|
|
|
361
357
|
```htmldjango
|
|
362
|
-
{%
|
|
358
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
363
359
|
Can you believe it's already <span>{{ date }}</span>??
|
|
364
|
-
{%
|
|
360
|
+
{% endcomponent_block %}
|
|
365
361
|
```
|
|
366
362
|
|
|
367
363
|
The rendered result (exactly the same as before):
|
|
@@ -381,32 +377,32 @@ You may be tempted to combine implicit fills with explicit `fill` tags. This wil
|
|
|
381
377
|
|
|
382
378
|
```htmldjango
|
|
383
379
|
{# DON'T DO THIS #}
|
|
384
|
-
{%
|
|
380
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
385
381
|
{% fill "header" %}Totally new header!{% endfill %}
|
|
386
382
|
Can you believe it's already <span>{{ date }}</span>??
|
|
387
|
-
{%
|
|
383
|
+
{% endcomponent_block %}
|
|
388
384
|
```
|
|
389
385
|
|
|
390
386
|
By contrast, it is permitted to use `fill` tags in nested components, e.g.:
|
|
391
387
|
|
|
392
388
|
```htmldjango
|
|
393
|
-
{%
|
|
394
|
-
{%
|
|
389
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
390
|
+
{% component_block "beautiful-box" %}
|
|
395
391
|
{% fill "content" %} Can you believe it's already <span>{{ date }}</span>?? {% endfill %}
|
|
396
|
-
{%
|
|
397
|
-
{%
|
|
392
|
+
{% endcomponent_block %}
|
|
393
|
+
{% endcomponent_block %}
|
|
398
394
|
```
|
|
399
395
|
|
|
400
396
|
This is fine too:
|
|
401
397
|
|
|
402
398
|
```htmldjango
|
|
403
|
-
{%
|
|
399
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
404
400
|
{% fill "header" %}
|
|
405
|
-
{%
|
|
401
|
+
{% component_block "calendar-header" %}
|
|
406
402
|
Super Special Calendar Header
|
|
407
|
-
{%
|
|
403
|
+
{% endcomponent_block %}
|
|
408
404
|
{% endfill %}
|
|
409
|
-
{%
|
|
405
|
+
{% endcomponent_block %}
|
|
410
406
|
```
|
|
411
407
|
|
|
412
408
|
### Components as views
|
|
@@ -483,9 +479,9 @@ If you're planning on passing an HTML string, check Django's use of [`format_htm
|
|
|
483
479
|
Certain properties of a slot can be accessed from within a 'fill' context. They are provided as attributes on a user-defined alias of the targeted slot. For instance, let's say you're filling a slot called 'body'. To access properties of this slot, alias it using the 'as' keyword to a new name -- or keep the original name. With the new slot alias, you can call `<alias>.default` to insert the default content.
|
|
484
480
|
|
|
485
481
|
```htmldjango
|
|
486
|
-
{%
|
|
482
|
+
{% component_block "calendar" date="2020-06-06" %}
|
|
487
483
|
{% fill "body" as "body" %}{{ body.default }}. Have a great day!{% endfill %}
|
|
488
|
-
{%
|
|
484
|
+
{% endcomponent_block %}
|
|
489
485
|
```
|
|
490
486
|
|
|
491
487
|
Produces:
|
|
@@ -618,10 +614,10 @@ COMPONENTS = {
|
|
|
618
614
|
|
|
619
615
|
## Component context and scope
|
|
620
616
|
|
|
621
|
-
By default, components can access context variables from the parent template, just like templates that are included with the `{% include %}` tag. Just like with `{% include %}`, if you don't want the component template to have access to the parent context, add `only` to the end of the `{% component %}` tag):
|
|
617
|
+
By default, components can access context variables from the parent template, just like templates that are included with the `{% include %}` tag. Just like with `{% include %}`, if you don't want the component template to have access to the parent context, add `only` to the end of the `{% component %}` (or `{% component_block %}` tag):
|
|
622
618
|
|
|
623
619
|
```htmldjango
|
|
624
|
-
{% component "calendar" date="2015-06-19" only %}
|
|
620
|
+
{% component "calendar" date="2015-06-19" only %}
|
|
625
621
|
```
|
|
626
622
|
|
|
627
623
|
NOTE: `{% csrf_token %}` tags need access to the top-level context, and they will not function properly if they are rendered in a component that is called with the `only` modifier.
|
|
@@ -26,7 +26,9 @@ class AppSettings:
|
|
|
26
26
|
|
|
27
27
|
@property
|
|
28
28
|
def CONTEXT_BEHAVIOR(self):
|
|
29
|
-
raw_value = self.settings.setdefault(
|
|
29
|
+
raw_value = self.settings.setdefault(
|
|
30
|
+
"context_behavior", ContextBehavior.GLOBAL.value
|
|
31
|
+
)
|
|
30
32
|
return self._validate_context_behavior(raw_value)
|
|
31
33
|
|
|
32
34
|
def _validate_context_behavior(self, raw_value):
|
|
@@ -34,7 +36,9 @@ class AppSettings:
|
|
|
34
36
|
return ContextBehavior(raw_value)
|
|
35
37
|
except ValueError:
|
|
36
38
|
valid_values = [behavior.value for behavior in ContextBehavior]
|
|
37
|
-
raise ValueError(
|
|
39
|
+
raise ValueError(
|
|
40
|
+
f"Invalid context behavior: {raw_value}. Valid options are {valid_values}"
|
|
41
|
+
)
|
|
38
42
|
|
|
39
43
|
|
|
40
44
|
app_settings = AppSettings()
|
|
@@ -80,7 +80,9 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
80
80
|
self,
|
|
81
81
|
registered_name: Optional[str] = None,
|
|
82
82
|
outer_context: Optional[Context] = None,
|
|
83
|
-
fill_content: Union[
|
|
83
|
+
fill_content: Union[
|
|
84
|
+
DefaultFillContent, Iterable[NamedFillContent]
|
|
85
|
+
] = (),
|
|
84
86
|
):
|
|
85
87
|
self.registered_name: Optional[str] = registered_name
|
|
86
88
|
self.outer_context: Context = outer_context or Context()
|
|
@@ -150,10 +152,14 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
150
152
|
if slots_data:
|
|
151
153
|
self._fill_slots(slots_data, escape_slots_content)
|
|
152
154
|
|
|
153
|
-
updated_filled_slots_context: FilledSlotsContext =
|
|
154
|
-
|
|
155
|
+
updated_filled_slots_context: FilledSlotsContext = (
|
|
156
|
+
self._process_template_and_update_filled_slot_context(
|
|
157
|
+
context, template
|
|
158
|
+
)
|
|
155
159
|
)
|
|
156
|
-
with context.update(
|
|
160
|
+
with context.update(
|
|
161
|
+
{FILLED_SLOTS_CONTENT_CONTEXT_KEY: updated_filled_slots_context}
|
|
162
|
+
):
|
|
157
163
|
return template.render(context)
|
|
158
164
|
|
|
159
165
|
def render_to_response(
|
|
@@ -195,14 +201,19 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
195
201
|
named_fills_content = {}
|
|
196
202
|
else:
|
|
197
203
|
default_fill_content = None
|
|
198
|
-
named_fills_content = {
|
|
204
|
+
named_fills_content = {
|
|
205
|
+
name: (nodelist, alias)
|
|
206
|
+
for name, nodelist, alias in self.fill_content
|
|
207
|
+
}
|
|
199
208
|
|
|
200
209
|
# If value is `None`, then slot is unfilled.
|
|
201
210
|
slot_name2fill_content: Dict[SlotName, Optional[FillContent]] = {}
|
|
202
211
|
default_slot_encountered: bool = False
|
|
203
212
|
required_slot_names: Set[str] = set()
|
|
204
213
|
|
|
205
|
-
for node in template.nodelist.get_nodes_by_type(
|
|
214
|
+
for node in template.nodelist.get_nodes_by_type(
|
|
215
|
+
(SlotNode, IfSlotFilledConditionBranchNode) # type: ignore
|
|
216
|
+
):
|
|
206
217
|
if isinstance(node, SlotNode):
|
|
207
218
|
# Give slot node knowledge of its parent template.
|
|
208
219
|
node.template = template
|
|
@@ -214,7 +225,9 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
214
225
|
f"To fix, check template '{template.name}' "
|
|
215
226
|
f"of component '{self.registered_name}'."
|
|
216
227
|
)
|
|
217
|
-
content_data: Optional[FillContent] =
|
|
228
|
+
content_data: Optional[FillContent] = (
|
|
229
|
+
None # `None` -> unfilled
|
|
230
|
+
)
|
|
218
231
|
if node.is_required:
|
|
219
232
|
required_slot_names.add(node.name)
|
|
220
233
|
if node.is_default:
|
|
@@ -232,19 +245,25 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
232
245
|
elif isinstance(node, IfSlotFilledConditionBranchNode):
|
|
233
246
|
node.template = template
|
|
234
247
|
else:
|
|
235
|
-
raise RuntimeError(
|
|
248
|
+
raise RuntimeError(
|
|
249
|
+
f"Node of {type(node).__name__} does not require linking."
|
|
250
|
+
)
|
|
236
251
|
|
|
237
252
|
# Check: Only component templates that include a 'default' slot
|
|
238
253
|
# can be invoked with implicit filling.
|
|
239
254
|
if default_fill_content and not default_slot_encountered:
|
|
240
255
|
raise TemplateSyntaxError(
|
|
241
|
-
f"Component '{self.registered_name}' passed default fill content
|
|
256
|
+
f"Component '{self.registered_name}' passed default fill content "
|
|
242
257
|
f"(i.e. without explicit 'fill' tag), "
|
|
243
258
|
f"even though none of its slots is marked as 'default'."
|
|
244
259
|
)
|
|
245
260
|
|
|
246
|
-
unfilled_slots: Set[str] = set(
|
|
247
|
-
|
|
261
|
+
unfilled_slots: Set[str] = set(
|
|
262
|
+
k for k, v in slot_name2fill_content.items() if v is None
|
|
263
|
+
)
|
|
264
|
+
unmatched_fills: Set[str] = (
|
|
265
|
+
named_fills_content.keys() - slot_name2fill_content.keys()
|
|
266
|
+
)
|
|
248
267
|
|
|
249
268
|
# Check that 'required' slots are filled.
|
|
250
269
|
for slot_name in unfilled_slots:
|
|
@@ -267,7 +286,9 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
267
286
|
# Higher values make matching stricter. This is probably preferable, as it
|
|
268
287
|
# reduces false positives.
|
|
269
288
|
for fill_name in unmatched_fills:
|
|
270
|
-
fuzzy_slot_name_matches = difflib.get_close_matches(
|
|
289
|
+
fuzzy_slot_name_matches = difflib.get_close_matches(
|
|
290
|
+
fill_name, unfilled_slots, n=1, cutoff=0.7
|
|
291
|
+
)
|
|
271
292
|
msg = (
|
|
272
293
|
f"Component '{self.registered_name}' passed fill "
|
|
273
294
|
f"that refers to undefined slot: '{fill_name}'."
|
|
@@ -284,7 +305,9 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
284
305
|
if content_data # Slots whose content is None (i.e. unfilled) are dropped.
|
|
285
306
|
}
|
|
286
307
|
try:
|
|
287
|
-
prev_context: FilledSlotsContext = context[
|
|
308
|
+
prev_context: FilledSlotsContext = context[
|
|
309
|
+
FILLED_SLOTS_CONTENT_CONTEXT_KEY
|
|
310
|
+
]
|
|
288
311
|
return prev_context.new_child(filled_slots_map)
|
|
289
312
|
except KeyError:
|
|
290
313
|
return ChainMap(filled_slots_map)
|
|
@@ -12,8 +12,13 @@ class ComponentRegistry(object):
|
|
|
12
12
|
|
|
13
13
|
def register(self, name=None, component=None):
|
|
14
14
|
existing_component = self._registry.get(name)
|
|
15
|
-
if
|
|
16
|
-
|
|
15
|
+
if (
|
|
16
|
+
existing_component
|
|
17
|
+
and existing_component.class_hash != component.class_hash
|
|
18
|
+
):
|
|
19
|
+
raise AlreadyRegistered(
|
|
20
|
+
'The component "%s" has already been registered' % name
|
|
21
|
+
)
|
|
17
22
|
self._registry[name] = component
|
|
18
23
|
|
|
19
24
|
def unregister(self, name):
|
|
@@ -9,7 +9,9 @@ class Command(BaseCommand):
|
|
|
9
9
|
help = "Creates a new component"
|
|
10
10
|
|
|
11
11
|
def add_arguments(self, parser):
|
|
12
|
-
parser.add_argument(
|
|
12
|
+
parser.add_argument(
|
|
13
|
+
"name", type=str, help="The name of the component to create"
|
|
14
|
+
)
|
|
13
15
|
parser.add_argument(
|
|
14
16
|
"--path",
|
|
15
17
|
type=str,
|
|
@@ -69,7 +71,9 @@ class Command(BaseCommand):
|
|
|
69
71
|
elif base_dir:
|
|
70
72
|
component_path = os.path.join(base_dir, "components", name)
|
|
71
73
|
else:
|
|
72
|
-
raise CommandError(
|
|
74
|
+
raise CommandError(
|
|
75
|
+
"You must specify a path or set BASE_DIR in your django settings"
|
|
76
|
+
)
|
|
73
77
|
|
|
74
78
|
if os.path.exists(component_path):
|
|
75
79
|
if force:
|
|
@@ -80,7 +84,11 @@ class Command(BaseCommand):
|
|
|
80
84
|
)
|
|
81
85
|
)
|
|
82
86
|
else:
|
|
83
|
-
self.stdout.write(
|
|
87
|
+
self.stdout.write(
|
|
88
|
+
self.style.WARNING(
|
|
89
|
+
f'The component "{name}" already exists. Overwriting...'
|
|
90
|
+
)
|
|
91
|
+
)
|
|
84
92
|
else:
|
|
85
93
|
raise CommandError(
|
|
86
94
|
f'The component "{name}" already exists at {component_path}. Use --force to overwrite.'
|
|
@@ -99,7 +107,9 @@ class Command(BaseCommand):
|
|
|
99
107
|
)
|
|
100
108
|
f.write(script_content.strip())
|
|
101
109
|
|
|
102
|
-
with open(
|
|
110
|
+
with open(
|
|
111
|
+
os.path.join(component_path, css_filename), "w"
|
|
112
|
+
) as f:
|
|
103
113
|
style_content = dedent(
|
|
104
114
|
f"""
|
|
105
115
|
.component-{name} {{
|
|
@@ -109,7 +119,9 @@ class Command(BaseCommand):
|
|
|
109
119
|
)
|
|
110
120
|
f.write(style_content.strip())
|
|
111
121
|
|
|
112
|
-
with open(
|
|
122
|
+
with open(
|
|
123
|
+
os.path.join(component_path, template_filename), "w"
|
|
124
|
+
) as f:
|
|
113
125
|
template_content = dedent(
|
|
114
126
|
f"""
|
|
115
127
|
<div class="component-{name}">
|
|
@@ -121,7 +133,9 @@ class Command(BaseCommand):
|
|
|
121
133
|
)
|
|
122
134
|
f.write(template_content.strip())
|
|
123
135
|
|
|
124
|
-
with open(
|
|
136
|
+
with open(
|
|
137
|
+
os.path.join(component_path, f"{name}.py"), "w"
|
|
138
|
+
) as f:
|
|
125
139
|
py_content = dedent(
|
|
126
140
|
f"""
|
|
127
141
|
from django_components import component
|
|
@@ -143,8 +157,16 @@ class Command(BaseCommand):
|
|
|
143
157
|
f.write(py_content.strip())
|
|
144
158
|
|
|
145
159
|
if verbose:
|
|
146
|
-
self.stdout.write(
|
|
160
|
+
self.stdout.write(
|
|
161
|
+
self.style.SUCCESS(
|
|
162
|
+
f"Successfully created {name} component at {component_path}"
|
|
163
|
+
)
|
|
164
|
+
)
|
|
147
165
|
else:
|
|
148
|
-
self.stdout.write(
|
|
166
|
+
self.stdout.write(
|
|
167
|
+
self.style.SUCCESS(
|
|
168
|
+
f"Successfully created {name} component"
|
|
169
|
+
)
|
|
170
|
+
)
|
|
149
171
|
else:
|
|
150
172
|
raise CommandError("You must specify a component name")
|
|
@@ -11,7 +11,9 @@ CSS_DEPENDENCY_PLACEHOLDER = '<link name="CSS_PLACEHOLDER">'
|
|
|
11
11
|
JS_DEPENDENCY_PLACEHOLDER = '<script name="JS_PLACEHOLDER"></script>'
|
|
12
12
|
|
|
13
13
|
SCRIPT_TAG_REGEX = re.compile("<script")
|
|
14
|
-
COMPONENT_COMMENT_REGEX = re.compile(
|
|
14
|
+
COMPONENT_COMMENT_REGEX = re.compile(
|
|
15
|
+
rb"<!-- _RENDERED (?P<name>[\w\-/]+?) -->"
|
|
16
|
+
)
|
|
15
17
|
PLACEHOLDER_REGEX = re.compile(
|
|
16
18
|
rb"<!-- _RENDERED (?P<name>[\w\-/]+?) -->"
|
|
17
19
|
rb'|<link name="CSS_PLACEHOLDER">'
|
|
@@ -30,7 +32,9 @@ class ComponentDependencyMiddleware:
|
|
|
30
32
|
def __call__(self, request):
|
|
31
33
|
response = self.get_response(request)
|
|
32
34
|
if (
|
|
33
|
-
getattr(settings, "COMPONENTS", {}).get(
|
|
35
|
+
getattr(settings, "COMPONENTS", {}).get(
|
|
36
|
+
"RENDER_DEPENDENCIES", False
|
|
37
|
+
)
|
|
34
38
|
and not isinstance(response, StreamingHttpResponse)
|
|
35
39
|
and response.get("Content-Type", "").startswith("text/html")
|
|
36
40
|
):
|
|
@@ -39,12 +43,23 @@ class ComponentDependencyMiddleware:
|
|
|
39
43
|
|
|
40
44
|
|
|
41
45
|
def process_response_content(content):
|
|
42
|
-
component_names_seen = {
|
|
43
|
-
|
|
46
|
+
component_names_seen = {
|
|
47
|
+
match.group("name")
|
|
48
|
+
for match in COMPONENT_COMMENT_REGEX.finditer(content)
|
|
49
|
+
}
|
|
50
|
+
all_components = [
|
|
51
|
+
registry.get(name.decode("utf-8"))("") for name in component_names_seen
|
|
52
|
+
]
|
|
44
53
|
all_media = join_media(all_components)
|
|
45
|
-
js_dependencies = b"".join(
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
js_dependencies = b"".join(
|
|
55
|
+
media.encode("utf-8") for media in all_media.render_js()
|
|
56
|
+
)
|
|
57
|
+
css_dependencies = b"".join(
|
|
58
|
+
media.encode("utf-8") for media in all_media.render_css()
|
|
59
|
+
)
|
|
60
|
+
return PLACEHOLDER_REGEX.sub(
|
|
61
|
+
DependencyReplacer(css_dependencies, js_dependencies), content
|
|
62
|
+
)
|
|
48
63
|
|
|
49
64
|
|
|
50
65
|
def add_module_attribute_to_scripts(scripts):
|