django-components 0.37__tar.gz → 0.51__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.37 → django_components-0.51}/PKG-INFO +45 -32
- {django_components-0.37 → django_components-0.51}/README.md +44 -31
- {django_components-0.37 → django_components-0.51}/django_components/app_settings.py +2 -6
- {django_components-0.37 → django_components-0.51}/django_components/component.py +13 -36
- {django_components-0.37 → django_components-0.51}/django_components/component_registry.py +2 -7
- {django_components-0.37 → django_components-0.51}/django_components/management/commands/startcomponent.py +8 -30
- django_components-0.51/django_components/management/commands/upgradecomponent.py +65 -0
- {django_components-0.37 → django_components-0.51}/django_components/middleware.py +7 -22
- {django_components-0.37 → django_components-0.51}/django_components/safer_staticfiles/apps.py +1 -3
- {django_components-0.37 → django_components-0.51}/django_components/templatetags/component_tags.py +48 -157
- django_components-0.51/django_components/types.py +21 -0
- {django_components-0.37 → django_components-0.51}/django_components.egg-info/PKG-INFO +45 -32
- {django_components-0.37 → django_components-0.51}/django_components.egg-info/SOURCES.txt +2 -1
- {django_components-0.37 → django_components-0.51}/pyproject.toml +16 -2
- django_components-0.51/setup.cfg +4 -0
- {django_components-0.37 → django_components-0.51}/setup.py +2 -4
- {django_components-0.37 → django_components-0.51}/tests/test_autodiscover.py +10 -47
- {django_components-0.37 → django_components-0.51}/tests/test_component.py +10 -18
- {django_components-0.37 → django_components-0.51}/tests/test_component_as_view.py +4 -10
- django_components-0.51/tests/test_context.py +386 -0
- {django_components-0.37 → django_components-0.51}/tests/test_dependency_rendering.py +48 -70
- {django_components-0.37 → django_components-0.51}/tests/test_registry.py +4 -12
- {django_components-0.37 → django_components-0.51}/tests/test_startcomponent_command.py +5 -15
- {django_components-0.37 → django_components-0.51}/tests/test_templatetags.py +169 -232
- {django_components-0.37 → django_components-0.51}/tests/testutils.py +3 -9
- django_components-0.37/setup.cfg +0 -17
- django_components-0.37/tests/test_context.py +0 -512
- {django_components-0.37 → django_components-0.51}/LICENSE +0 -0
- {django_components-0.37 → django_components-0.51}/django_components/__init__.py +0 -0
- {django_components-0.37 → django_components-0.51}/django_components/apps.py +0 -0
- {django_components-0.37 → django_components-0.51}/django_components/management/__init__.py +0 -0
- {django_components-0.37 → django_components-0.51}/django_components/management/commands/__init__.py +0 -0
- {django_components-0.37 → django_components-0.51}/django_components/safer_staticfiles/__init__.py +0 -0
- {django_components-0.37 → django_components-0.51}/django_components/template_loader.py +0 -0
- {django_components-0.37 → django_components-0.51}/django_components/templatetags/__init__.py +0 -0
- {django_components-0.37 → django_components-0.51}/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.37 → django_components-0.51}/django_components.egg-info/requires.txt +0 -0
- {django_components-0.37 → django_components-0.51}/django_components.egg-info/top_level.txt +0 -0
- {django_components-0.37 → django_components-0.51}/tests/components/__init__.py +0 -0
- {django_components-0.37 → django_components-0.51}/tests/components/single_file.py +0 -0
- {django_components-0.37 → django_components-0.51}/tests/components/urls.py +0 -0
- {django_components-0.37 → django_components-0.51}/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.51
|
|
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" %}{% endcomponent %}
|
|
40
40
|
```
|
|
41
41
|
|
|
42
42
|
And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
@@ -49,17 +49,21 @@ Read on to learn about the details!
|
|
|
49
49
|
|
|
50
50
|
## Release notes
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
🚨📢 **Version 0.5** CHANGES THE SYNTAX for components. `component_block` is now `component`, and `component` blocks need an ending `endcomponent` tag. The new `python manage.py upgradecomponent` command can be used to upgrade a directory (use --path argument to point to each dir) of components to the new syntax automatically.
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
This change is done to simplify the API in anticipation of a 1.0 release of django_components. After 1.0 we intend to be stricter with big changes like this in point releases.
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
**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.
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
**Version 0.28** introduces 'implicit' slot filling and the `default` option for `slot` tags.
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
**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).
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
**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 featuPpre to have access to. Hoping that this will feel worth it!
|
|
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.
|
|
63
67
|
|
|
64
68
|
## Security notes 🚨
|
|
65
69
|
|
|
@@ -249,7 +253,7 @@ First load the `component_tags` tag library, then use the `component_[js/css]_de
|
|
|
249
253
|
{% component_css_dependencies %}
|
|
250
254
|
</head>
|
|
251
255
|
<body>
|
|
252
|
-
{% component "calendar" date="2015-06-19" %}
|
|
256
|
+
{% component "calendar" date="2015-06-19" %}{% endcomponent %}
|
|
253
257
|
{% component_js_dependencies %}
|
|
254
258
|
</body>
|
|
255
259
|
<html>
|
|
@@ -280,6 +284,7 @@ Components can also be defined in a single file, which is useful for small compo
|
|
|
280
284
|
```python
|
|
281
285
|
# In a file called [project root]/components/calendar.py
|
|
282
286
|
from django_components import component
|
|
287
|
+
from django_components import types as t
|
|
283
288
|
|
|
284
289
|
@component.register("calendar")
|
|
285
290
|
class Calendar(component.Component):
|
|
@@ -288,16 +293,16 @@ class Calendar(component.Component):
|
|
|
288
293
|
"date": date,
|
|
289
294
|
}
|
|
290
295
|
|
|
291
|
-
template = """
|
|
296
|
+
template: t.django_html = """
|
|
292
297
|
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
|
293
298
|
"""
|
|
294
299
|
|
|
295
|
-
css = """
|
|
300
|
+
css: t.css = """
|
|
296
301
|
.calendar-component { width: 200px; background: pink; }
|
|
297
302
|
.calendar-component span { font-weight: bold; }
|
|
298
303
|
"""
|
|
299
304
|
|
|
300
|
-
js = """
|
|
305
|
+
js: t.js = """
|
|
301
306
|
(function(){
|
|
302
307
|
if (document.querySelector(".calendar-component")) {
|
|
303
308
|
document.querySelector(".calendar-component").onclick = function(){ alert("Clicked calendar!"); };
|
|
@@ -308,6 +313,8 @@ class Calendar(component.Component):
|
|
|
308
313
|
|
|
309
314
|
This makes it easy to create small components without having to create a separate template, CSS, and JS file.
|
|
310
315
|
|
|
316
|
+
Note that the `t.django_html`, `t.css`, and `t.js` types are used to specify the type of the template, CSS, and JS files, respectively. This is not necessary, but if you're using VSCode with the [Python Inline Source Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=samwillis.python-inline-source) extension, it will give you syntax highlighting for the template, CSS, and JS.
|
|
317
|
+
|
|
311
318
|
## Using slots in templates
|
|
312
319
|
|
|
313
320
|
_New in version 0.26_:
|
|
@@ -326,7 +333,7 @@ This mechanism makes components more reusable and composable.
|
|
|
326
333
|
In the example below we introduce two block tags that work hand in hand to make this work. These are...
|
|
327
334
|
|
|
328
335
|
- `{% slot <name> %}`/`{% endslot %}`: Declares a new slot in the component template.
|
|
329
|
-
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `
|
|
336
|
+
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `component` tag pair.) Fills a declared slot with the specified content.
|
|
330
337
|
|
|
331
338
|
Let's update our calendar component to support more customization. We'll add `slot` tag pairs to its template, _calendar.html_.
|
|
332
339
|
|
|
@@ -344,9 +351,9 @@ Let's update our calendar component to support more customization. We'll add `sl
|
|
|
344
351
|
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:
|
|
345
352
|
|
|
346
353
|
```htmldjango
|
|
347
|
-
{%
|
|
354
|
+
{% component "calendar" date="2020-06-06" %}
|
|
348
355
|
{% fill "body" %}Can you believe it's already <span>{{ date }}</span>??{% endfill %}
|
|
349
|
-
{%
|
|
356
|
+
{% endcomponent %}
|
|
350
357
|
```
|
|
351
358
|
|
|
352
359
|
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:
|
|
@@ -364,7 +371,7 @@ Since the header block is unspecified, it's taken from the base template. If you
|
|
|
364
371
|
|
|
365
372
|
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.
|
|
366
373
|
|
|
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 `
|
|
374
|
+
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` 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`.
|
|
368
375
|
|
|
369
376
|
Here's the same example as before, except with default slots and implicit filling.
|
|
370
377
|
|
|
@@ -384,9 +391,9 @@ The template:
|
|
|
384
391
|
Including the component (notice how the `fill` tag is omitted):
|
|
385
392
|
|
|
386
393
|
```htmldjango
|
|
387
|
-
{%
|
|
394
|
+
{% component "calendar" date="2020-06-06" %}
|
|
388
395
|
Can you believe it's already <span>{{ date }}</span>??
|
|
389
|
-
{%
|
|
396
|
+
{% endcomponent %}
|
|
390
397
|
```
|
|
391
398
|
|
|
392
399
|
The rendered result (exactly the same as before):
|
|
@@ -406,32 +413,32 @@ You may be tempted to combine implicit fills with explicit `fill` tags. This wil
|
|
|
406
413
|
|
|
407
414
|
```htmldjango
|
|
408
415
|
{# DON'T DO THIS #}
|
|
409
|
-
{%
|
|
416
|
+
{% component "calendar" date="2020-06-06" %}
|
|
410
417
|
{% fill "header" %}Totally new header!{% endfill %}
|
|
411
418
|
Can you believe it's already <span>{{ date }}</span>??
|
|
412
|
-
{%
|
|
419
|
+
{% endcomponent %}
|
|
413
420
|
```
|
|
414
421
|
|
|
415
422
|
By contrast, it is permitted to use `fill` tags in nested components, e.g.:
|
|
416
423
|
|
|
417
424
|
```htmldjango
|
|
418
|
-
{%
|
|
419
|
-
{%
|
|
425
|
+
{% component "calendar" date="2020-06-06" %}
|
|
426
|
+
{% component "beautiful-box" %}
|
|
420
427
|
{% fill "content" %} Can you believe it's already <span>{{ date }}</span>?? {% endfill %}
|
|
421
|
-
{%
|
|
422
|
-
{%
|
|
428
|
+
{% endcomponent %}
|
|
429
|
+
{% endcomponent %}
|
|
423
430
|
```
|
|
424
431
|
|
|
425
432
|
This is fine too:
|
|
426
433
|
|
|
427
434
|
```htmldjango
|
|
428
|
-
{%
|
|
435
|
+
{% component "calendar" date="2020-06-06" %}
|
|
429
436
|
{% fill "header" %}
|
|
430
|
-
{%
|
|
437
|
+
{% component "calendar-header" %}
|
|
431
438
|
Super Special Calendar Header
|
|
432
|
-
{%
|
|
439
|
+
{% endcomponent %}
|
|
433
440
|
{% endfill %}
|
|
434
|
-
{%
|
|
441
|
+
{% endcomponent %}
|
|
435
442
|
```
|
|
436
443
|
|
|
437
444
|
### Components as views
|
|
@@ -508,9 +515,9 @@ If you're planning on passing an HTML string, check Django's use of [`format_htm
|
|
|
508
515
|
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.
|
|
509
516
|
|
|
510
517
|
```htmldjango
|
|
511
|
-
{%
|
|
518
|
+
{% component "calendar" date="2020-06-06" %}
|
|
512
519
|
{% fill "body" as "body" %}{{ body.default }}. Have a great day!{% endfill %}
|
|
513
|
-
{%
|
|
520
|
+
{% endcomponent %}
|
|
514
521
|
```
|
|
515
522
|
|
|
516
523
|
Produces:
|
|
@@ -643,10 +650,10 @@ COMPONENTS = {
|
|
|
643
650
|
|
|
644
651
|
## Component context and scope
|
|
645
652
|
|
|
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 %}`
|
|
653
|
+
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):
|
|
647
654
|
|
|
648
655
|
```htmldjango
|
|
649
|
-
{% component "calendar" date="2015-06-19" only %}
|
|
656
|
+
{% component "calendar" date="2015-06-19" only %}{% endcomponent %}
|
|
650
657
|
```
|
|
651
658
|
|
|
652
659
|
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.
|
|
@@ -777,6 +784,12 @@ python manage.py startcomponent my_component --dry-run
|
|
|
777
784
|
|
|
778
785
|
This will simulate the creation of `my_component` without creating any files.
|
|
779
786
|
|
|
787
|
+
## Community examples
|
|
788
|
+
|
|
789
|
+
One of our goals with `django-components` is to make it easy to share components between projects. If you have a set of components that you think would be useful to others, please open a pull request to add them to the list below.
|
|
790
|
+
|
|
791
|
+
- [django-htmx-components](https://github.com/iwanalabs/django-htmx-components): A set of components for use with [htmx](https://htmx.org/). Try out the [live demo](https://dhc.iwanalabs.com/).
|
|
792
|
+
|
|
780
793
|
## Install locally and run the tests
|
|
781
794
|
|
|
782
795
|
Start by forking the project by clicking the **Fork button** up in the right corner in the GitHub . This makes a copy of the repository in your own name. Now you can clone this repository locally and start adding features:
|
|
@@ -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" %}{% endcomponent %}
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
@@ -20,17 +20,21 @@ Read on to learn about the details!
|
|
|
20
20
|
|
|
21
21
|
## Release notes
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
🚨📢 **Version 0.5** CHANGES THE SYNTAX for components. `component_block` is now `component`, and `component` blocks need an ending `endcomponent` tag. The new `python manage.py upgradecomponent` command can be used to upgrade a directory (use --path argument to point to each dir) of components to the new syntax automatically.
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
This change is done to simplify the API in anticipation of a 1.0 release of django_components. After 1.0 we intend to be stricter with big changes like this in point releases.
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
**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.
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
**Version 0.28** introduces 'implicit' slot filling and the `default` option for `slot` tags.
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
**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).
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
**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 featuPpre to have access to. Hoping that this will feel worth it!
|
|
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.
|
|
34
38
|
|
|
35
39
|
## Security notes 🚨
|
|
36
40
|
|
|
@@ -220,7 +224,7 @@ First load the `component_tags` tag library, then use the `component_[js/css]_de
|
|
|
220
224
|
{% component_css_dependencies %}
|
|
221
225
|
</head>
|
|
222
226
|
<body>
|
|
223
|
-
{% component "calendar" date="2015-06-19" %}
|
|
227
|
+
{% component "calendar" date="2015-06-19" %}{% endcomponent %}
|
|
224
228
|
{% component_js_dependencies %}
|
|
225
229
|
</body>
|
|
226
230
|
<html>
|
|
@@ -251,6 +255,7 @@ Components can also be defined in a single file, which is useful for small compo
|
|
|
251
255
|
```python
|
|
252
256
|
# In a file called [project root]/components/calendar.py
|
|
253
257
|
from django_components import component
|
|
258
|
+
from django_components import types as t
|
|
254
259
|
|
|
255
260
|
@component.register("calendar")
|
|
256
261
|
class Calendar(component.Component):
|
|
@@ -259,16 +264,16 @@ class Calendar(component.Component):
|
|
|
259
264
|
"date": date,
|
|
260
265
|
}
|
|
261
266
|
|
|
262
|
-
template = """
|
|
267
|
+
template: t.django_html = """
|
|
263
268
|
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
|
264
269
|
"""
|
|
265
270
|
|
|
266
|
-
css = """
|
|
271
|
+
css: t.css = """
|
|
267
272
|
.calendar-component { width: 200px; background: pink; }
|
|
268
273
|
.calendar-component span { font-weight: bold; }
|
|
269
274
|
"""
|
|
270
275
|
|
|
271
|
-
js = """
|
|
276
|
+
js: t.js = """
|
|
272
277
|
(function(){
|
|
273
278
|
if (document.querySelector(".calendar-component")) {
|
|
274
279
|
document.querySelector(".calendar-component").onclick = function(){ alert("Clicked calendar!"); };
|
|
@@ -279,6 +284,8 @@ class Calendar(component.Component):
|
|
|
279
284
|
|
|
280
285
|
This makes it easy to create small components without having to create a separate template, CSS, and JS file.
|
|
281
286
|
|
|
287
|
+
Note that the `t.django_html`, `t.css`, and `t.js` types are used to specify the type of the template, CSS, and JS files, respectively. This is not necessary, but if you're using VSCode with the [Python Inline Source Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=samwillis.python-inline-source) extension, it will give you syntax highlighting for the template, CSS, and JS.
|
|
288
|
+
|
|
282
289
|
## Using slots in templates
|
|
283
290
|
|
|
284
291
|
_New in version 0.26_:
|
|
@@ -297,7 +304,7 @@ This mechanism makes components more reusable and composable.
|
|
|
297
304
|
In the example below we introduce two block tags that work hand in hand to make this work. These are...
|
|
298
305
|
|
|
299
306
|
- `{% slot <name> %}`/`{% endslot %}`: Declares a new slot in the component template.
|
|
300
|
-
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `
|
|
307
|
+
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `component` tag pair.) Fills a declared slot with the specified content.
|
|
301
308
|
|
|
302
309
|
Let's update our calendar component to support more customization. We'll add `slot` tag pairs to its template, _calendar.html_.
|
|
303
310
|
|
|
@@ -315,9 +322,9 @@ Let's update our calendar component to support more customization. We'll add `sl
|
|
|
315
322
|
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:
|
|
316
323
|
|
|
317
324
|
```htmldjango
|
|
318
|
-
{%
|
|
325
|
+
{% component "calendar" date="2020-06-06" %}
|
|
319
326
|
{% fill "body" %}Can you believe it's already <span>{{ date }}</span>??{% endfill %}
|
|
320
|
-
{%
|
|
327
|
+
{% endcomponent %}
|
|
321
328
|
```
|
|
322
329
|
|
|
323
330
|
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:
|
|
@@ -335,7 +342,7 @@ Since the header block is unspecified, it's taken from the base template. If you
|
|
|
335
342
|
|
|
336
343
|
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.
|
|
337
344
|
|
|
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 `
|
|
345
|
+
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` 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`.
|
|
339
346
|
|
|
340
347
|
Here's the same example as before, except with default slots and implicit filling.
|
|
341
348
|
|
|
@@ -355,9 +362,9 @@ The template:
|
|
|
355
362
|
Including the component (notice how the `fill` tag is omitted):
|
|
356
363
|
|
|
357
364
|
```htmldjango
|
|
358
|
-
{%
|
|
365
|
+
{% component "calendar" date="2020-06-06" %}
|
|
359
366
|
Can you believe it's already <span>{{ date }}</span>??
|
|
360
|
-
{%
|
|
367
|
+
{% endcomponent %}
|
|
361
368
|
```
|
|
362
369
|
|
|
363
370
|
The rendered result (exactly the same as before):
|
|
@@ -377,32 +384,32 @@ You may be tempted to combine implicit fills with explicit `fill` tags. This wil
|
|
|
377
384
|
|
|
378
385
|
```htmldjango
|
|
379
386
|
{# DON'T DO THIS #}
|
|
380
|
-
{%
|
|
387
|
+
{% component "calendar" date="2020-06-06" %}
|
|
381
388
|
{% fill "header" %}Totally new header!{% endfill %}
|
|
382
389
|
Can you believe it's already <span>{{ date }}</span>??
|
|
383
|
-
{%
|
|
390
|
+
{% endcomponent %}
|
|
384
391
|
```
|
|
385
392
|
|
|
386
393
|
By contrast, it is permitted to use `fill` tags in nested components, e.g.:
|
|
387
394
|
|
|
388
395
|
```htmldjango
|
|
389
|
-
{%
|
|
390
|
-
{%
|
|
396
|
+
{% component "calendar" date="2020-06-06" %}
|
|
397
|
+
{% component "beautiful-box" %}
|
|
391
398
|
{% fill "content" %} Can you believe it's already <span>{{ date }}</span>?? {% endfill %}
|
|
392
|
-
{%
|
|
393
|
-
{%
|
|
399
|
+
{% endcomponent %}
|
|
400
|
+
{% endcomponent %}
|
|
394
401
|
```
|
|
395
402
|
|
|
396
403
|
This is fine too:
|
|
397
404
|
|
|
398
405
|
```htmldjango
|
|
399
|
-
{%
|
|
406
|
+
{% component "calendar" date="2020-06-06" %}
|
|
400
407
|
{% fill "header" %}
|
|
401
|
-
{%
|
|
408
|
+
{% component "calendar-header" %}
|
|
402
409
|
Super Special Calendar Header
|
|
403
|
-
{%
|
|
410
|
+
{% endcomponent %}
|
|
404
411
|
{% endfill %}
|
|
405
|
-
{%
|
|
412
|
+
{% endcomponent %}
|
|
406
413
|
```
|
|
407
414
|
|
|
408
415
|
### Components as views
|
|
@@ -479,9 +486,9 @@ If you're planning on passing an HTML string, check Django's use of [`format_htm
|
|
|
479
486
|
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.
|
|
480
487
|
|
|
481
488
|
```htmldjango
|
|
482
|
-
{%
|
|
489
|
+
{% component "calendar" date="2020-06-06" %}
|
|
483
490
|
{% fill "body" as "body" %}{{ body.default }}. Have a great day!{% endfill %}
|
|
484
|
-
{%
|
|
491
|
+
{% endcomponent %}
|
|
485
492
|
```
|
|
486
493
|
|
|
487
494
|
Produces:
|
|
@@ -614,10 +621,10 @@ COMPONENTS = {
|
|
|
614
621
|
|
|
615
622
|
## Component context and scope
|
|
616
623
|
|
|
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 %}`
|
|
624
|
+
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):
|
|
618
625
|
|
|
619
626
|
```htmldjango
|
|
620
|
-
{% component "calendar" date="2015-06-19" only %}
|
|
627
|
+
{% component "calendar" date="2015-06-19" only %}{% endcomponent %}
|
|
621
628
|
```
|
|
622
629
|
|
|
623
630
|
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.
|
|
@@ -748,6 +755,12 @@ python manage.py startcomponent my_component --dry-run
|
|
|
748
755
|
|
|
749
756
|
This will simulate the creation of `my_component` without creating any files.
|
|
750
757
|
|
|
758
|
+
## Community examples
|
|
759
|
+
|
|
760
|
+
One of our goals with `django-components` is to make it easy to share components between projects. If you have a set of components that you think would be useful to others, please open a pull request to add them to the list below.
|
|
761
|
+
|
|
762
|
+
- [django-htmx-components](https://github.com/iwanalabs/django-htmx-components): A set of components for use with [htmx](https://htmx.org/). Try out the [live demo](https://dhc.iwanalabs.com/).
|
|
763
|
+
|
|
751
764
|
## Install locally and run the tests
|
|
752
765
|
|
|
753
766
|
Start by forking the project by clicking the **Fork button** up in the right corner in the GitHub . This makes a copy of the repository in your own name. Now you can clone this repository locally and start adding features:
|
|
@@ -26,9 +26,7 @@ class AppSettings:
|
|
|
26
26
|
|
|
27
27
|
@property
|
|
28
28
|
def CONTEXT_BEHAVIOR(self):
|
|
29
|
-
raw_value = self.settings.setdefault(
|
|
30
|
-
"context_behavior", ContextBehavior.GLOBAL.value
|
|
31
|
-
)
|
|
29
|
+
raw_value = self.settings.setdefault("context_behavior", ContextBehavior.GLOBAL.value)
|
|
32
30
|
return self._validate_context_behavior(raw_value)
|
|
33
31
|
|
|
34
32
|
def _validate_context_behavior(self, raw_value):
|
|
@@ -36,9 +34,7 @@ class AppSettings:
|
|
|
36
34
|
return ContextBehavior(raw_value)
|
|
37
35
|
except ValueError:
|
|
38
36
|
valid_values = [behavior.value for behavior in ContextBehavior]
|
|
39
|
-
raise ValueError(
|
|
40
|
-
f"Invalid context behavior: {raw_value}. Valid options are {valid_values}"
|
|
41
|
-
)
|
|
37
|
+
raise ValueError(f"Invalid context behavior: {raw_value}. Valid options are {valid_values}")
|
|
42
38
|
|
|
43
39
|
|
|
44
40
|
app_settings = AppSettings()
|
|
@@ -80,9 +80,7 @@ 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[
|
|
84
|
-
DefaultFillContent, Iterable[NamedFillContent]
|
|
85
|
-
] = (),
|
|
83
|
+
fill_content: Union[DefaultFillContent, Iterable[NamedFillContent]] = (),
|
|
86
84
|
):
|
|
87
85
|
self.registered_name: Optional[str] = registered_name
|
|
88
86
|
self.outer_context: Context = outer_context or Context()
|
|
@@ -152,14 +150,10 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
152
150
|
if slots_data:
|
|
153
151
|
self._fill_slots(slots_data, escape_slots_content)
|
|
154
152
|
|
|
155
|
-
updated_filled_slots_context: FilledSlotsContext = (
|
|
156
|
-
|
|
157
|
-
context, template
|
|
158
|
-
)
|
|
153
|
+
updated_filled_slots_context: FilledSlotsContext = self._process_template_and_update_filled_slot_context(
|
|
154
|
+
context, template
|
|
159
155
|
)
|
|
160
|
-
with context.update(
|
|
161
|
-
{FILLED_SLOTS_CONTENT_CONTEXT_KEY: updated_filled_slots_context}
|
|
162
|
-
):
|
|
156
|
+
with context.update({FILLED_SLOTS_CONTENT_CONTEXT_KEY: updated_filled_slots_context}):
|
|
163
157
|
return template.render(context)
|
|
164
158
|
|
|
165
159
|
def render_to_response(
|
|
@@ -201,19 +195,14 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
201
195
|
named_fills_content = {}
|
|
202
196
|
else:
|
|
203
197
|
default_fill_content = None
|
|
204
|
-
named_fills_content = {
|
|
205
|
-
name: (nodelist, alias)
|
|
206
|
-
for name, nodelist, alias in self.fill_content
|
|
207
|
-
}
|
|
198
|
+
named_fills_content = {name: (nodelist, alias) for name, nodelist, alias in self.fill_content}
|
|
208
199
|
|
|
209
200
|
# If value is `None`, then slot is unfilled.
|
|
210
201
|
slot_name2fill_content: Dict[SlotName, Optional[FillContent]] = {}
|
|
211
202
|
default_slot_encountered: bool = False
|
|
212
203
|
required_slot_names: Set[str] = set()
|
|
213
204
|
|
|
214
|
-
for node in template.nodelist.get_nodes_by_type(
|
|
215
|
-
(SlotNode, IfSlotFilledConditionBranchNode) # type: ignore
|
|
216
|
-
):
|
|
205
|
+
for node in template.nodelist.get_nodes_by_type((SlotNode, IfSlotFilledConditionBranchNode)): # type: ignore
|
|
217
206
|
if isinstance(node, SlotNode):
|
|
218
207
|
# Give slot node knowledge of its parent template.
|
|
219
208
|
node.template = template
|
|
@@ -225,9 +214,7 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
225
214
|
f"To fix, check template '{template.name}' "
|
|
226
215
|
f"of component '{self.registered_name}'."
|
|
227
216
|
)
|
|
228
|
-
content_data: Optional[FillContent] =
|
|
229
|
-
None # `None` -> unfilled
|
|
230
|
-
)
|
|
217
|
+
content_data: Optional[FillContent] = None # `None` -> unfilled
|
|
231
218
|
if node.is_required:
|
|
232
219
|
required_slot_names.add(node.name)
|
|
233
220
|
if node.is_default:
|
|
@@ -245,25 +232,19 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
245
232
|
elif isinstance(node, IfSlotFilledConditionBranchNode):
|
|
246
233
|
node.template = template
|
|
247
234
|
else:
|
|
248
|
-
raise RuntimeError(
|
|
249
|
-
f"Node of {type(node).__name__} does not require linking."
|
|
250
|
-
)
|
|
235
|
+
raise RuntimeError(f"Node of {type(node).__name__} does not require linking.")
|
|
251
236
|
|
|
252
237
|
# Check: Only component templates that include a 'default' slot
|
|
253
238
|
# can be invoked with implicit filling.
|
|
254
239
|
if default_fill_content and not default_slot_encountered:
|
|
255
240
|
raise TemplateSyntaxError(
|
|
256
|
-
f"Component '{self.registered_name}' passed default fill content "
|
|
241
|
+
f"Component '{self.registered_name}' passed default fill content '{default_fill_content}'"
|
|
257
242
|
f"(i.e. without explicit 'fill' tag), "
|
|
258
243
|
f"even though none of its slots is marked as 'default'."
|
|
259
244
|
)
|
|
260
245
|
|
|
261
|
-
unfilled_slots: Set[str] = set(
|
|
262
|
-
|
|
263
|
-
)
|
|
264
|
-
unmatched_fills: Set[str] = (
|
|
265
|
-
named_fills_content.keys() - slot_name2fill_content.keys()
|
|
266
|
-
)
|
|
246
|
+
unfilled_slots: Set[str] = set(k for k, v in slot_name2fill_content.items() if v is None)
|
|
247
|
+
unmatched_fills: Set[str] = named_fills_content.keys() - slot_name2fill_content.keys()
|
|
267
248
|
|
|
268
249
|
# Check that 'required' slots are filled.
|
|
269
250
|
for slot_name in unfilled_slots:
|
|
@@ -286,9 +267,7 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
286
267
|
# Higher values make matching stricter. This is probably preferable, as it
|
|
287
268
|
# reduces false positives.
|
|
288
269
|
for fill_name in unmatched_fills:
|
|
289
|
-
fuzzy_slot_name_matches = difflib.get_close_matches(
|
|
290
|
-
fill_name, unfilled_slots, n=1, cutoff=0.7
|
|
291
|
-
)
|
|
270
|
+
fuzzy_slot_name_matches = difflib.get_close_matches(fill_name, unfilled_slots, n=1, cutoff=0.7)
|
|
292
271
|
msg = (
|
|
293
272
|
f"Component '{self.registered_name}' passed fill "
|
|
294
273
|
f"that refers to undefined slot: '{fill_name}'."
|
|
@@ -305,9 +284,7 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
305
284
|
if content_data # Slots whose content is None (i.e. unfilled) are dropped.
|
|
306
285
|
}
|
|
307
286
|
try:
|
|
308
|
-
prev_context: FilledSlotsContext = context[
|
|
309
|
-
FILLED_SLOTS_CONTENT_CONTEXT_KEY
|
|
310
|
-
]
|
|
287
|
+
prev_context: FilledSlotsContext = context[FILLED_SLOTS_CONTENT_CONTEXT_KEY]
|
|
311
288
|
return prev_context.new_child(filled_slots_map)
|
|
312
289
|
except KeyError:
|
|
313
290
|
return ChainMap(filled_slots_map)
|
|
@@ -12,13 +12,8 @@ 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
|
-
|
|
17
|
-
and existing_component.class_hash != component.class_hash
|
|
18
|
-
):
|
|
19
|
-
raise AlreadyRegistered(
|
|
20
|
-
'The component "%s" has already been registered' % name
|
|
21
|
-
)
|
|
15
|
+
if existing_component and existing_component.class_hash != component.class_hash:
|
|
16
|
+
raise AlreadyRegistered('The component "%s" has already been registered' % name)
|
|
22
17
|
self._registry[name] = component
|
|
23
18
|
|
|
24
19
|
def unregister(self, name):
|