django-components 0.35__tar.gz → 0.36__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.
Potentially problematic release.
This version of django-components might be problematic. Click here for more details.
- {django_components-0.35 → django_components-0.36}/PKG-INFO +33 -29
- {django_components-0.35 → django_components-0.36}/README.md +32 -28
- {django_components-0.35 → django_components-0.36}/django_components/app_settings.py +2 -6
- {django_components-0.35 → django_components-0.36}/django_components/component.py +13 -36
- {django_components-0.35 → django_components-0.36}/django_components/component_registry.py +2 -7
- {django_components-0.35 → django_components-0.36}/django_components/management/commands/startcomponent.py +8 -30
- django_components-0.36/django_components/management/commands/upgradecomponent.py +65 -0
- {django_components-0.35 → django_components-0.36}/django_components/middleware.py +7 -22
- {django_components-0.35 → django_components-0.36}/django_components/safer_staticfiles/apps.py +1 -3
- {django_components-0.35 → django_components-0.36}/django_components/templatetags/component_tags.py +48 -157
- {django_components-0.35 → django_components-0.36}/django_components.egg-info/PKG-INFO +33 -29
- {django_components-0.35 → django_components-0.36}/django_components.egg-info/SOURCES.txt +1 -1
- {django_components-0.35 → django_components-0.36}/pyproject.toml +16 -2
- django_components-0.36/setup.cfg +4 -0
- {django_components-0.35 → django_components-0.36}/setup.py +2 -4
- {django_components-0.35 → django_components-0.36}/tests/test_autodiscover.py +10 -47
- {django_components-0.35 → django_components-0.36}/tests/test_component.py +10 -18
- {django_components-0.35 → django_components-0.36}/tests/test_component_as_view.py +4 -10
- django_components-0.36/tests/test_context.py +386 -0
- {django_components-0.35 → django_components-0.36}/tests/test_dependency_rendering.py +48 -70
- {django_components-0.35 → django_components-0.36}/tests/test_registry.py +4 -12
- {django_components-0.35 → django_components-0.36}/tests/test_startcomponent_command.py +5 -15
- {django_components-0.35 → django_components-0.36}/tests/test_templatetags.py +169 -232
- {django_components-0.35 → django_components-0.36}/tests/testutils.py +3 -9
- django_components-0.35/setup.cfg +0 -17
- django_components-0.35/tests/test_context.py +0 -512
- {django_components-0.35 → django_components-0.36}/LICENSE +0 -0
- {django_components-0.35 → django_components-0.36}/django_components/__init__.py +0 -0
- {django_components-0.35 → django_components-0.36}/django_components/apps.py +0 -0
- {django_components-0.35 → django_components-0.36}/django_components/management/__init__.py +0 -0
- {django_components-0.35 → django_components-0.36}/django_components/management/commands/__init__.py +0 -0
- {django_components-0.35 → django_components-0.36}/django_components/safer_staticfiles/__init__.py +0 -0
- {django_components-0.35 → django_components-0.36}/django_components/template_loader.py +0 -0
- {django_components-0.35 → django_components-0.36}/django_components/templatetags/__init__.py +0 -0
- {django_components-0.35 → django_components-0.36}/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.35 → django_components-0.36}/django_components.egg-info/requires.txt +0 -0
- {django_components-0.35 → django_components-0.36}/django_components.egg-info/top_level.txt +0 -0
- {django_components-0.35 → django_components-0.36}/tests/components/__init__.py +0 -0
- {django_components-0.35 → django_components-0.36}/tests/components/single_file.py +0 -0
- {django_components-0.35 → django_components-0.36}/tests/components/urls.py +0 -0
- {django_components-0.35 → django_components-0.36}/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.36
|
|
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
|
+
<span style="color: red; font-weight: bold">**Version 0.36** CHANGES THE SYNTAX</span> 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>
|
|
@@ -326,7 +330,7 @@ This mechanism makes components more reusable and composable.
|
|
|
326
330
|
In the example below we introduce two block tags that work hand in hand to make this work. These are...
|
|
327
331
|
|
|
328
332
|
- `{% slot <name> %}`/`{% endslot %}`: Declares a new slot in the component template.
|
|
329
|
-
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `
|
|
333
|
+
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `component` tag pair.) Fills a declared slot with the specified content.
|
|
330
334
|
|
|
331
335
|
Let's update our calendar component to support more customization. We'll add `slot` tag pairs to its template, _calendar.html_.
|
|
332
336
|
|
|
@@ -344,9 +348,9 @@ Let's update our calendar component to support more customization. We'll add `sl
|
|
|
344
348
|
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
349
|
|
|
346
350
|
```htmldjango
|
|
347
|
-
{%
|
|
351
|
+
{% component "calendar" date="2020-06-06" %}
|
|
348
352
|
{% fill "body" %}Can you believe it's already <span>{{ date }}</span>??{% endfill %}
|
|
349
|
-
{%
|
|
353
|
+
{% endcomponent %}
|
|
350
354
|
```
|
|
351
355
|
|
|
352
356
|
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 +368,7 @@ Since the header block is unspecified, it's taken from the base template. If you
|
|
|
364
368
|
|
|
365
369
|
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
370
|
|
|
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 `
|
|
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 `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
372
|
|
|
369
373
|
Here's the same example as before, except with default slots and implicit filling.
|
|
370
374
|
|
|
@@ -384,9 +388,9 @@ The template:
|
|
|
384
388
|
Including the component (notice how the `fill` tag is omitted):
|
|
385
389
|
|
|
386
390
|
```htmldjango
|
|
387
|
-
{%
|
|
391
|
+
{% component "calendar" date="2020-06-06" %}
|
|
388
392
|
Can you believe it's already <span>{{ date }}</span>??
|
|
389
|
-
{%
|
|
393
|
+
{% endcomponent %}
|
|
390
394
|
```
|
|
391
395
|
|
|
392
396
|
The rendered result (exactly the same as before):
|
|
@@ -406,32 +410,32 @@ You may be tempted to combine implicit fills with explicit `fill` tags. This wil
|
|
|
406
410
|
|
|
407
411
|
```htmldjango
|
|
408
412
|
{# DON'T DO THIS #}
|
|
409
|
-
{%
|
|
413
|
+
{% component "calendar" date="2020-06-06" %}
|
|
410
414
|
{% fill "header" %}Totally new header!{% endfill %}
|
|
411
415
|
Can you believe it's already <span>{{ date }}</span>??
|
|
412
|
-
{%
|
|
416
|
+
{% endcomponent %}
|
|
413
417
|
```
|
|
414
418
|
|
|
415
419
|
By contrast, it is permitted to use `fill` tags in nested components, e.g.:
|
|
416
420
|
|
|
417
421
|
```htmldjango
|
|
418
|
-
{%
|
|
419
|
-
{%
|
|
422
|
+
{% component "calendar" date="2020-06-06" %}
|
|
423
|
+
{% component "beautiful-box" %}
|
|
420
424
|
{% fill "content" %} Can you believe it's already <span>{{ date }}</span>?? {% endfill %}
|
|
421
|
-
{%
|
|
422
|
-
{%
|
|
425
|
+
{% endcomponent %}
|
|
426
|
+
{% endcomponent %}
|
|
423
427
|
```
|
|
424
428
|
|
|
425
429
|
This is fine too:
|
|
426
430
|
|
|
427
431
|
```htmldjango
|
|
428
|
-
{%
|
|
432
|
+
{% component "calendar" date="2020-06-06" %}
|
|
429
433
|
{% fill "header" %}
|
|
430
|
-
{%
|
|
434
|
+
{% component "calendar-header" %}
|
|
431
435
|
Super Special Calendar Header
|
|
432
|
-
{%
|
|
436
|
+
{% endcomponent %}
|
|
433
437
|
{% endfill %}
|
|
434
|
-
{%
|
|
438
|
+
{% endcomponent %}
|
|
435
439
|
```
|
|
436
440
|
|
|
437
441
|
### Components as views
|
|
@@ -508,9 +512,9 @@ If you're planning on passing an HTML string, check Django's use of [`format_htm
|
|
|
508
512
|
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
513
|
|
|
510
514
|
```htmldjango
|
|
511
|
-
{%
|
|
515
|
+
{% component "calendar" date="2020-06-06" %}
|
|
512
516
|
{% fill "body" as "body" %}{{ body.default }}. Have a great day!{% endfill %}
|
|
513
|
-
{%
|
|
517
|
+
{% endcomponent %}
|
|
514
518
|
```
|
|
515
519
|
|
|
516
520
|
Produces:
|
|
@@ -643,10 +647,10 @@ COMPONENTS = {
|
|
|
643
647
|
|
|
644
648
|
## Component context and scope
|
|
645
649
|
|
|
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 %}`
|
|
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):
|
|
647
651
|
|
|
648
652
|
```htmldjango
|
|
649
|
-
{% component "calendar" date="2015-06-19" only %}
|
|
653
|
+
{% component "calendar" date="2015-06-19" only %}{% endcomponent %}
|
|
650
654
|
```
|
|
651
655
|
|
|
652
656
|
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" %}{% 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
|
+
<span style="color: red; font-weight: bold">**Version 0.36** CHANGES THE SYNTAX</span> 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>
|
|
@@ -297,7 +301,7 @@ This mechanism makes components more reusable and composable.
|
|
|
297
301
|
In the example below we introduce two block tags that work hand in hand to make this work. These are...
|
|
298
302
|
|
|
299
303
|
- `{% slot <name> %}`/`{% endslot %}`: Declares a new slot in the component template.
|
|
300
|
-
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `
|
|
304
|
+
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `component` tag pair.) Fills a declared slot with the specified content.
|
|
301
305
|
|
|
302
306
|
Let's update our calendar component to support more customization. We'll add `slot` tag pairs to its template, _calendar.html_.
|
|
303
307
|
|
|
@@ -315,9 +319,9 @@ Let's update our calendar component to support more customization. We'll add `sl
|
|
|
315
319
|
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
320
|
|
|
317
321
|
```htmldjango
|
|
318
|
-
{%
|
|
322
|
+
{% component "calendar" date="2020-06-06" %}
|
|
319
323
|
{% fill "body" %}Can you believe it's already <span>{{ date }}</span>??{% endfill %}
|
|
320
|
-
{%
|
|
324
|
+
{% endcomponent %}
|
|
321
325
|
```
|
|
322
326
|
|
|
323
327
|
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 +339,7 @@ Since the header block is unspecified, it's taken from the base template. If you
|
|
|
335
339
|
|
|
336
340
|
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
341
|
|
|
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 `
|
|
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 `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
343
|
|
|
340
344
|
Here's the same example as before, except with default slots and implicit filling.
|
|
341
345
|
|
|
@@ -355,9 +359,9 @@ The template:
|
|
|
355
359
|
Including the component (notice how the `fill` tag is omitted):
|
|
356
360
|
|
|
357
361
|
```htmldjango
|
|
358
|
-
{%
|
|
362
|
+
{% component "calendar" date="2020-06-06" %}
|
|
359
363
|
Can you believe it's already <span>{{ date }}</span>??
|
|
360
|
-
{%
|
|
364
|
+
{% endcomponent %}
|
|
361
365
|
```
|
|
362
366
|
|
|
363
367
|
The rendered result (exactly the same as before):
|
|
@@ -377,32 +381,32 @@ You may be tempted to combine implicit fills with explicit `fill` tags. This wil
|
|
|
377
381
|
|
|
378
382
|
```htmldjango
|
|
379
383
|
{# DON'T DO THIS #}
|
|
380
|
-
{%
|
|
384
|
+
{% component "calendar" date="2020-06-06" %}
|
|
381
385
|
{% fill "header" %}Totally new header!{% endfill %}
|
|
382
386
|
Can you believe it's already <span>{{ date }}</span>??
|
|
383
|
-
{%
|
|
387
|
+
{% endcomponent %}
|
|
384
388
|
```
|
|
385
389
|
|
|
386
390
|
By contrast, it is permitted to use `fill` tags in nested components, e.g.:
|
|
387
391
|
|
|
388
392
|
```htmldjango
|
|
389
|
-
{%
|
|
390
|
-
{%
|
|
393
|
+
{% component "calendar" date="2020-06-06" %}
|
|
394
|
+
{% component "beautiful-box" %}
|
|
391
395
|
{% fill "content" %} Can you believe it's already <span>{{ date }}</span>?? {% endfill %}
|
|
392
|
-
{%
|
|
393
|
-
{%
|
|
396
|
+
{% endcomponent %}
|
|
397
|
+
{% endcomponent %}
|
|
394
398
|
```
|
|
395
399
|
|
|
396
400
|
This is fine too:
|
|
397
401
|
|
|
398
402
|
```htmldjango
|
|
399
|
-
{%
|
|
403
|
+
{% component "calendar" date="2020-06-06" %}
|
|
400
404
|
{% fill "header" %}
|
|
401
|
-
{%
|
|
405
|
+
{% component "calendar-header" %}
|
|
402
406
|
Super Special Calendar Header
|
|
403
|
-
{%
|
|
407
|
+
{% endcomponent %}
|
|
404
408
|
{% endfill %}
|
|
405
|
-
{%
|
|
409
|
+
{% endcomponent %}
|
|
406
410
|
```
|
|
407
411
|
|
|
408
412
|
### Components as views
|
|
@@ -479,9 +483,9 @@ If you're planning on passing an HTML string, check Django's use of [`format_htm
|
|
|
479
483
|
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
484
|
|
|
481
485
|
```htmldjango
|
|
482
|
-
{%
|
|
486
|
+
{% component "calendar" date="2020-06-06" %}
|
|
483
487
|
{% fill "body" as "body" %}{{ body.default }}. Have a great day!{% endfill %}
|
|
484
|
-
{%
|
|
488
|
+
{% endcomponent %}
|
|
485
489
|
```
|
|
486
490
|
|
|
487
491
|
Produces:
|
|
@@ -614,10 +618,10 @@ COMPONENTS = {
|
|
|
614
618
|
|
|
615
619
|
## Component context and scope
|
|
616
620
|
|
|
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 %}`
|
|
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):
|
|
618
622
|
|
|
619
623
|
```htmldjango
|
|
620
|
-
{% component "calendar" date="2015-06-19" only %}
|
|
624
|
+
{% component "calendar" date="2015-06-19" only %}{% endcomponent %}
|
|
621
625
|
```
|
|
622
626
|
|
|
623
627
|
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,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):
|
|
@@ -9,9 +9,7 @@ class Command(BaseCommand):
|
|
|
9
9
|
help = "Creates a new component"
|
|
10
10
|
|
|
11
11
|
def add_arguments(self, parser):
|
|
12
|
-
parser.add_argument(
|
|
13
|
-
"name", type=str, help="The name of the component to create"
|
|
14
|
-
)
|
|
12
|
+
parser.add_argument("name", type=str, help="The name of the component to create")
|
|
15
13
|
parser.add_argument(
|
|
16
14
|
"--path",
|
|
17
15
|
type=str,
|
|
@@ -71,9 +69,7 @@ class Command(BaseCommand):
|
|
|
71
69
|
elif base_dir:
|
|
72
70
|
component_path = os.path.join(base_dir, "components", name)
|
|
73
71
|
else:
|
|
74
|
-
raise CommandError(
|
|
75
|
-
"You must specify a path or set BASE_DIR in your django settings"
|
|
76
|
-
)
|
|
72
|
+
raise CommandError("You must specify a path or set BASE_DIR in your django settings")
|
|
77
73
|
|
|
78
74
|
if os.path.exists(component_path):
|
|
79
75
|
if force:
|
|
@@ -84,11 +80,7 @@ class Command(BaseCommand):
|
|
|
84
80
|
)
|
|
85
81
|
)
|
|
86
82
|
else:
|
|
87
|
-
self.stdout.write(
|
|
88
|
-
self.style.WARNING(
|
|
89
|
-
f'The component "{name}" already exists. Overwriting...'
|
|
90
|
-
)
|
|
91
|
-
)
|
|
83
|
+
self.stdout.write(self.style.WARNING(f'The component "{name}" already exists. Overwriting...'))
|
|
92
84
|
else:
|
|
93
85
|
raise CommandError(
|
|
94
86
|
f'The component "{name}" already exists at {component_path}. Use --force to overwrite.'
|
|
@@ -107,9 +99,7 @@ class Command(BaseCommand):
|
|
|
107
99
|
)
|
|
108
100
|
f.write(script_content.strip())
|
|
109
101
|
|
|
110
|
-
with open(
|
|
111
|
-
os.path.join(component_path, css_filename), "w"
|
|
112
|
-
) as f:
|
|
102
|
+
with open(os.path.join(component_path, css_filename), "w") as f:
|
|
113
103
|
style_content = dedent(
|
|
114
104
|
f"""
|
|
115
105
|
.component-{name} {{
|
|
@@ -119,9 +109,7 @@ class Command(BaseCommand):
|
|
|
119
109
|
)
|
|
120
110
|
f.write(style_content.strip())
|
|
121
111
|
|
|
122
|
-
with open(
|
|
123
|
-
os.path.join(component_path, template_filename), "w"
|
|
124
|
-
) as f:
|
|
112
|
+
with open(os.path.join(component_path, template_filename), "w") as f:
|
|
125
113
|
template_content = dedent(
|
|
126
114
|
f"""
|
|
127
115
|
<div class="component-{name}">
|
|
@@ -133,9 +121,7 @@ class Command(BaseCommand):
|
|
|
133
121
|
)
|
|
134
122
|
f.write(template_content.strip())
|
|
135
123
|
|
|
136
|
-
with open(
|
|
137
|
-
os.path.join(component_path, f"{name}.py"), "w"
|
|
138
|
-
) as f:
|
|
124
|
+
with open(os.path.join(component_path, f"{name}.py"), "w") as f:
|
|
139
125
|
py_content = dedent(
|
|
140
126
|
f"""
|
|
141
127
|
from django_components import component
|
|
@@ -157,16 +143,8 @@ class Command(BaseCommand):
|
|
|
157
143
|
f.write(py_content.strip())
|
|
158
144
|
|
|
159
145
|
if verbose:
|
|
160
|
-
self.stdout.write(
|
|
161
|
-
self.style.SUCCESS(
|
|
162
|
-
f"Successfully created {name} component at {component_path}"
|
|
163
|
-
)
|
|
164
|
-
)
|
|
146
|
+
self.stdout.write(self.style.SUCCESS(f"Successfully created {name} component at {component_path}"))
|
|
165
147
|
else:
|
|
166
|
-
self.stdout.write(
|
|
167
|
-
self.style.SUCCESS(
|
|
168
|
-
f"Successfully created {name} component"
|
|
169
|
-
)
|
|
170
|
-
)
|
|
148
|
+
self.stdout.write(self.style.SUCCESS(f"Successfully created {name} component"))
|
|
171
149
|
else:
|
|
172
150
|
raise CommandError("You must specify a component name")
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import re
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from django.conf import settings
|
|
6
|
+
from django.core.management.base import BaseCommand
|
|
7
|
+
from django.template.engine import Engine
|
|
8
|
+
|
|
9
|
+
from django_components.template_loader import Loader
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Command(BaseCommand):
|
|
13
|
+
help = "Updates component and component_block tags to the new syntax"
|
|
14
|
+
|
|
15
|
+
def add_arguments(self, parser):
|
|
16
|
+
parser.add_argument("--path", type=str, help="Path to search for components")
|
|
17
|
+
|
|
18
|
+
def handle(self, *args, **options):
|
|
19
|
+
current_engine = Engine.get_default()
|
|
20
|
+
loader = Loader(current_engine)
|
|
21
|
+
dirs = loader.get_dirs()
|
|
22
|
+
|
|
23
|
+
if settings.BASE_DIR:
|
|
24
|
+
dirs.append(Path(settings.BASE_DIR) / "templates")
|
|
25
|
+
|
|
26
|
+
if options["path"]:
|
|
27
|
+
dirs = [options["path"]]
|
|
28
|
+
|
|
29
|
+
for dir_path in dirs:
|
|
30
|
+
self.stdout.write(f"Searching for components in {dir_path}...")
|
|
31
|
+
for root, _, files in os.walk(dir_path):
|
|
32
|
+
for file in files:
|
|
33
|
+
if file.endswith((".html", ".py")):
|
|
34
|
+
file_path = os.path.join(root, file)
|
|
35
|
+
with open(file_path, "r+", encoding="utf-8") as f:
|
|
36
|
+
content = f.read()
|
|
37
|
+
content_with_closed_components, step0_count = re.subn(
|
|
38
|
+
r'({%\s*component\s*"(\w+?)"(.*?)%})(?!.*?{%\s*endcomponent\s*%})',
|
|
39
|
+
r"\1{% endcomponent %}",
|
|
40
|
+
content,
|
|
41
|
+
flags=re.DOTALL,
|
|
42
|
+
)
|
|
43
|
+
updated_content, step1_count_opening = re.subn(
|
|
44
|
+
r'{%\s*component_block\s*"(\w+?)"\s*(.*?)%}',
|
|
45
|
+
r'{% component "\1" \2%}',
|
|
46
|
+
content_with_closed_components,
|
|
47
|
+
flags=re.DOTALL,
|
|
48
|
+
)
|
|
49
|
+
updated_content, step2_count_closing = re.subn(
|
|
50
|
+
r'{%\s*endcomponent_block\s*"(\w+?)"\s*%}',
|
|
51
|
+
r"{% endcomponent %}",
|
|
52
|
+
updated_content,
|
|
53
|
+
flags=re.DOTALL,
|
|
54
|
+
)
|
|
55
|
+
updated_content, step2_count_closing_no_name = re.subn(
|
|
56
|
+
r"{%\s*endcomponent_block\s*%}", r"{% endcomponent %}", updated_content, flags=re.DOTALL
|
|
57
|
+
)
|
|
58
|
+
total_updates = (
|
|
59
|
+
step0_count + step1_count_opening + step2_count_closing + step2_count_closing_no_name
|
|
60
|
+
)
|
|
61
|
+
if total_updates > 0:
|
|
62
|
+
f.seek(0)
|
|
63
|
+
f.write(updated_content)
|
|
64
|
+
f.truncate()
|
|
65
|
+
self.stdout.write(f"Updated {file_path}: {total_updates} changes made")
|