django-components 0.77__tar.gz → 0.79__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.77/src/django_components.egg-info → django_components-0.79}/PKG-INFO +62 -42
- {django_components-0.77 → django_components-0.79}/README.md +61 -41
- {django_components-0.77 → django_components-0.79}/pyproject.toml +1 -1
- {django_components-0.77 → django_components-0.79}/src/django_components/app_settings.py +1 -1
- {django_components-0.77 → django_components-0.79}/src/django_components/attributes.py +9 -6
- {django_components-0.77 → django_components-0.79}/src/django_components/component.py +53 -0
- {django_components-0.77 → django_components-0.79/src/django_components.egg-info}/PKG-INFO +62 -42
- {django_components-0.77 → django_components-0.79}/src/django_components.egg-info/SOURCES.txt +5 -1
- {django_components-0.77 → django_components-0.79}/tests/test_attributes.py +3 -3
- django_components-0.79/tests/test_component.py +128 -0
- {django_components-0.77 → django_components-0.79}/tests/test_component_as_view.py +25 -1
- django_components-0.79/tests/test_component_media.py +388 -0
- {django_components-0.77 → django_components-0.79}/tests/test_context.py +257 -127
- {django_components-0.77 → django_components-0.79}/tests/test_dependency_rendering.py +3 -1
- {django_components-0.77 → django_components-0.79}/tests/test_template_parser.py +17 -15
- django_components-0.79/tests/test_templatetags.py +455 -0
- django_components-0.79/tests/test_templatetags_component.py +417 -0
- django_components-0.79/tests/test_templatetags_slot_fill.py +1294 -0
- django_components-0.79/tests/test_templatetags_templating.py +826 -0
- django_components-0.79/tests/testutils.py +176 -0
- django_components-0.77/tests/test_component.py +0 -1021
- django_components-0.77/tests/test_templatetags.py +0 -2699
- django_components-0.77/tests/testutils.py +0 -77
- {django_components-0.77 → django_components-0.79}/LICENSE +0 -0
- {django_components-0.77 → django_components-0.79}/setup.cfg +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/__init__.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/apps.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/component_registry.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/context.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/expression.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/logger.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/management/__init__.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/management/commands/__init__.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/management/commands/startcomponent.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/management/commands/upgradecomponent.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/middleware.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/node.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/py.typed +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/safer_staticfiles/__init__.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/safer_staticfiles/apps.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/slots.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/template_loader.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/template_parser.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/templatetags/__init__.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/templatetags/component_tags.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/types.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components/utils.py +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components.egg-info/requires.txt +0 -0
- {django_components-0.77 → django_components-0.79}/src/django_components.egg-info/top_level.txt +0 -0
- {django_components-0.77 → django_components-0.79}/tests/test_autodiscover.py +0 -0
- /django_components-0.77/tests/test_startcomponent_command.py → /django_components-0.79/tests/test_command_startcomponent.py +0 -0
- {django_components-0.77 → django_components-0.79}/tests/test_registry.py +0 -0
- {django_components-0.77 → django_components-0.79}/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.79
|
|
4
4
|
Summary: A way to create simple reusable template components in Django.
|
|
5
5
|
Author-email: Emil Stenström <emil@emilstenstrom.se>
|
|
6
6
|
License: MIT
|
|
@@ -68,6 +68,11 @@ Read on to learn about the details!
|
|
|
68
68
|
|
|
69
69
|
## Release notes
|
|
70
70
|
|
|
71
|
+
🚨📢 **Version 0.79**
|
|
72
|
+
|
|
73
|
+
- BREAKING CHANGE: Default value for the `COMPONENTS.context_behavior` setting was changes from `"isolated"` to `"django"`. If you did not set this value explicitly before, this may be a breaking change. See the rationale for change [here](https://github.com/EmilStenstrom/django-components/issues/498).
|
|
74
|
+
|
|
75
|
+
|
|
71
76
|
🚨📢 **Version 0.77** CHANGED the syntax for accessing default slot content.
|
|
72
77
|
- Previously, the syntax was
|
|
73
78
|
`{% fill "my_slot" as "alias" %}` and `{{ alias.default }}`.
|
|
@@ -1384,9 +1389,11 @@ attributes_to_string(attrs)
|
|
|
1384
1389
|
|
|
1385
1390
|
## Component context and scope
|
|
1386
1391
|
|
|
1387
|
-
By default,
|
|
1392
|
+
By default, context variables are passed down the template as in regular Django - deeper scopes can access the variables from the outer scopes. So if you have several nested forloops, then inside the deep-most loop you can access variables defined by all previous loops.
|
|
1388
1393
|
|
|
1389
|
-
|
|
1394
|
+
With this in mind, the `{% component %}` tag behaves similarly to `{% include %}` tag - inside the component tag, you can access all variables that were defined outside of it.
|
|
1395
|
+
|
|
1396
|
+
And just like with `{% include %}`, if you don't want a specific component template to have access to the parent context, add `only` to the end of the `{% component %}` tag:
|
|
1390
1397
|
|
|
1391
1398
|
```htmldjango
|
|
1392
1399
|
{% component "calendar" date="2015-06-19" only %}{% endcomponent %}
|
|
@@ -1394,7 +1401,9 @@ You can set the [context_behavior](#context-behavior) option to `"django"`, to m
|
|
|
1394
1401
|
|
|
1395
1402
|
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.
|
|
1396
1403
|
|
|
1397
|
-
|
|
1404
|
+
If you find yourself using the `only` modifier often, you can set the [context_behavior](#context-behavior) option to `"isolated"`, which automatically applies the `only` modifier. This is useful if you want to make sure that components don't accidentally access the outer context.
|
|
1405
|
+
|
|
1406
|
+
Components can also access the outer context in their context methods like `get_context_data` by accessing the property `self.outer_context`.
|
|
1398
1407
|
|
|
1399
1408
|
## Rendering JS and CSS dependencies
|
|
1400
1409
|
|
|
@@ -1472,15 +1481,25 @@ COMPONENTS = {
|
|
|
1472
1481
|
|
|
1473
1482
|
> NOTE: `context_behavior` and `slot_context_behavior` options were merged in v0.70.
|
|
1474
1483
|
>
|
|
1475
|
-
> If you are migrating from BEFORE v0.67, set `context_behavior` to `"django"`. From v0.67
|
|
1484
|
+
> If you are migrating from BEFORE v0.67, set `context_behavior` to `"django"`. From v0.67 to v0.78 (incl) the default value was `"isolated"`.
|
|
1485
|
+
>
|
|
1486
|
+
> For v0.79 and later, the default is again `"django"`. See the rationale for change [here](https://github.com/EmilStenstrom/django-components/issues/498).
|
|
1476
1487
|
|
|
1477
1488
|
You can configure what variables are available inside the `{% fill %}` tags. See [Component context and scope](#component-context-and-scope).
|
|
1478
1489
|
|
|
1479
1490
|
This has two modes:
|
|
1480
1491
|
|
|
1481
|
-
- `"django"` -
|
|
1492
|
+
- `"django"` - Default - The default Django template behavior.
|
|
1482
1493
|
|
|
1483
|
-
|
|
1494
|
+
Inside the `{% fill %}` tag, the context variables you can access are a union of:
|
|
1495
|
+
- All the variables that were OUTSIDE the fill tag, including any loops or with tag
|
|
1496
|
+
- Data returned from `get_context_data()` of the component that wraps the fill tag.
|
|
1497
|
+
|
|
1498
|
+
- `"isolated"` - Similar behavior to [Vue](https://vuejs.org/guide/components/slots.html#render-scope) or React, this is useful if you want to make sure that components don't accidentally access variables defined outside of the component.
|
|
1499
|
+
|
|
1500
|
+
Inside the `{% fill %}` tag, you can ONLY access variables from 2 places:
|
|
1501
|
+
- `get_context_data()` of the component which defined the template (AKA the "root" component)
|
|
1502
|
+
- Any loops (`{% for ... %}`) that the `{% fill %}` tag is part of.
|
|
1484
1503
|
|
|
1485
1504
|
```python
|
|
1486
1505
|
COMPONENTS = {
|
|
@@ -1490,75 +1509,76 @@ COMPONENTS = {
|
|
|
1490
1509
|
|
|
1491
1510
|
#### Example "django"
|
|
1492
1511
|
|
|
1493
|
-
Given this template
|
|
1494
|
-
|
|
1495
|
-
```django
|
|
1496
|
-
{% with cheese="feta" %}
|
|
1497
|
-
{% component 'my_comp' %}
|
|
1498
|
-
{{ my_var }} # my_var
|
|
1499
|
-
{{ cheese }} # cheese
|
|
1500
|
-
{% endcomponent %}
|
|
1501
|
-
{% endwith %}
|
|
1502
|
-
```
|
|
1503
|
-
|
|
1504
|
-
and this context returned from the `get_context_data()` method
|
|
1512
|
+
Given this template:
|
|
1505
1513
|
|
|
1506
1514
|
```py
|
|
1507
|
-
|
|
1515
|
+
class RootComp(component.Component):
|
|
1516
|
+
template = """
|
|
1517
|
+
{% with cheese="feta" %}
|
|
1518
|
+
{% component 'my_comp' %}
|
|
1519
|
+
{{ my_var }} # my_var
|
|
1520
|
+
{{ cheese }} # cheese
|
|
1521
|
+
{% endcomponent %}
|
|
1522
|
+
{% endwith %}
|
|
1523
|
+
"""
|
|
1524
|
+
def get_context_data(self):
|
|
1525
|
+
return { "my_var": 123 }
|
|
1508
1526
|
```
|
|
1509
1527
|
|
|
1510
|
-
Then if component "my_comp"
|
|
1528
|
+
Then if `get_context_data()` of the component `"my_comp"` returns following data:
|
|
1511
1529
|
|
|
1512
1530
|
```py
|
|
1513
1531
|
{ "my_var": 456 }
|
|
1514
1532
|
```
|
|
1515
1533
|
|
|
1516
|
-
Then
|
|
1534
|
+
Then the template will be rendered as:
|
|
1517
1535
|
|
|
1518
1536
|
```django
|
|
1519
1537
|
456 # my_var
|
|
1520
1538
|
feta # cheese
|
|
1521
1539
|
```
|
|
1522
1540
|
|
|
1523
|
-
Because "my_comp"
|
|
1541
|
+
Because `"my_comp"` overshadows the variable `"my_var"`,
|
|
1524
1542
|
so `{{ my_var }}` equals `456`.
|
|
1525
|
-
And variable "cheese" equals `feta`, because the fill CAN access
|
|
1526
|
-
the current context.
|
|
1527
1543
|
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
Given this template
|
|
1544
|
+
And variable `"cheese"` equals `feta`, because the fill CAN access
|
|
1545
|
+
all the data defined in the outer layers, like the `{% with %}` tag.
|
|
1531
1546
|
|
|
1532
|
-
|
|
1533
|
-
{% with cheese="feta" %}
|
|
1534
|
-
{% component 'my_comp' %}
|
|
1535
|
-
{{ my_var }} # my_var
|
|
1536
|
-
{{ cheese }} # cheese
|
|
1537
|
-
{% endcomponent %}
|
|
1538
|
-
{% endwith %}
|
|
1539
|
-
```
|
|
1547
|
+
#### Example "isolated"
|
|
1540
1548
|
|
|
1541
|
-
|
|
1549
|
+
Given this template:
|
|
1542
1550
|
|
|
1543
1551
|
```py
|
|
1544
|
-
|
|
1552
|
+
class RootComp(component.Component):
|
|
1553
|
+
template = """
|
|
1554
|
+
{% with cheese="feta" %}
|
|
1555
|
+
{% component 'my_comp' %}
|
|
1556
|
+
{{ my_var }} # my_var
|
|
1557
|
+
{{ cheese }} # cheese
|
|
1558
|
+
{% endcomponent %}
|
|
1559
|
+
{% endwith %}
|
|
1560
|
+
"""
|
|
1561
|
+
def get_context_data(self):
|
|
1562
|
+
return { "my_var": 123 }
|
|
1545
1563
|
```
|
|
1546
1564
|
|
|
1547
|
-
Then if component "my_comp"
|
|
1565
|
+
Then if `get_context_data()` of the component `"my_comp"` returns following data:
|
|
1548
1566
|
|
|
1549
1567
|
```py
|
|
1550
1568
|
{ "my_var": 456 }
|
|
1551
1569
|
```
|
|
1552
1570
|
|
|
1553
|
-
Then
|
|
1571
|
+
Then the template will be rendered as:
|
|
1554
1572
|
|
|
1555
1573
|
```django
|
|
1556
1574
|
123 # my_var
|
|
1557
1575
|
# cheese
|
|
1558
1576
|
```
|
|
1559
1577
|
|
|
1560
|
-
Because
|
|
1561
|
-
But since "cheese" is not defined there, it's empty.
|
|
1578
|
+
Because variables `"my_var"` and `"cheese"` are searched only inside `RootComponent.get_context_data()`.
|
|
1579
|
+
But since `"cheese"` is not defined there, it's empty.
|
|
1580
|
+
|
|
1581
|
+
Notice that the variables defined with the `{% with %}` tag are ignored inside the `{% fill %}` tag with the `"isolated"` mode.
|
|
1562
1582
|
|
|
1563
1583
|
## Logging and debugging
|
|
1564
1584
|
|
|
@@ -44,6 +44,11 @@ Read on to learn about the details!
|
|
|
44
44
|
|
|
45
45
|
## Release notes
|
|
46
46
|
|
|
47
|
+
🚨📢 **Version 0.79**
|
|
48
|
+
|
|
49
|
+
- BREAKING CHANGE: Default value for the `COMPONENTS.context_behavior` setting was changes from `"isolated"` to `"django"`. If you did not set this value explicitly before, this may be a breaking change. See the rationale for change [here](https://github.com/EmilStenstrom/django-components/issues/498).
|
|
50
|
+
|
|
51
|
+
|
|
47
52
|
🚨📢 **Version 0.77** CHANGED the syntax for accessing default slot content.
|
|
48
53
|
- Previously, the syntax was
|
|
49
54
|
`{% fill "my_slot" as "alias" %}` and `{{ alias.default }}`.
|
|
@@ -1360,9 +1365,11 @@ attributes_to_string(attrs)
|
|
|
1360
1365
|
|
|
1361
1366
|
## Component context and scope
|
|
1362
1367
|
|
|
1363
|
-
By default,
|
|
1368
|
+
By default, context variables are passed down the template as in regular Django - deeper scopes can access the variables from the outer scopes. So if you have several nested forloops, then inside the deep-most loop you can access variables defined by all previous loops.
|
|
1364
1369
|
|
|
1365
|
-
|
|
1370
|
+
With this in mind, the `{% component %}` tag behaves similarly to `{% include %}` tag - inside the component tag, you can access all variables that were defined outside of it.
|
|
1371
|
+
|
|
1372
|
+
And just like with `{% include %}`, if you don't want a specific component template to have access to the parent context, add `only` to the end of the `{% component %}` tag:
|
|
1366
1373
|
|
|
1367
1374
|
```htmldjango
|
|
1368
1375
|
{% component "calendar" date="2015-06-19" only %}{% endcomponent %}
|
|
@@ -1370,7 +1377,9 @@ You can set the [context_behavior](#context-behavior) option to `"django"`, to m
|
|
|
1370
1377
|
|
|
1371
1378
|
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.
|
|
1372
1379
|
|
|
1373
|
-
|
|
1380
|
+
If you find yourself using the `only` modifier often, you can set the [context_behavior](#context-behavior) option to `"isolated"`, which automatically applies the `only` modifier. This is useful if you want to make sure that components don't accidentally access the outer context.
|
|
1381
|
+
|
|
1382
|
+
Components can also access the outer context in their context methods like `get_context_data` by accessing the property `self.outer_context`.
|
|
1374
1383
|
|
|
1375
1384
|
## Rendering JS and CSS dependencies
|
|
1376
1385
|
|
|
@@ -1448,15 +1457,25 @@ COMPONENTS = {
|
|
|
1448
1457
|
|
|
1449
1458
|
> NOTE: `context_behavior` and `slot_context_behavior` options were merged in v0.70.
|
|
1450
1459
|
>
|
|
1451
|
-
> If you are migrating from BEFORE v0.67, set `context_behavior` to `"django"`. From v0.67
|
|
1460
|
+
> If you are migrating from BEFORE v0.67, set `context_behavior` to `"django"`. From v0.67 to v0.78 (incl) the default value was `"isolated"`.
|
|
1461
|
+
>
|
|
1462
|
+
> For v0.79 and later, the default is again `"django"`. See the rationale for change [here](https://github.com/EmilStenstrom/django-components/issues/498).
|
|
1452
1463
|
|
|
1453
1464
|
You can configure what variables are available inside the `{% fill %}` tags. See [Component context and scope](#component-context-and-scope).
|
|
1454
1465
|
|
|
1455
1466
|
This has two modes:
|
|
1456
1467
|
|
|
1457
|
-
- `"django"` -
|
|
1468
|
+
- `"django"` - Default - The default Django template behavior.
|
|
1458
1469
|
|
|
1459
|
-
|
|
1470
|
+
Inside the `{% fill %}` tag, the context variables you can access are a union of:
|
|
1471
|
+
- All the variables that were OUTSIDE the fill tag, including any loops or with tag
|
|
1472
|
+
- Data returned from `get_context_data()` of the component that wraps the fill tag.
|
|
1473
|
+
|
|
1474
|
+
- `"isolated"` - Similar behavior to [Vue](https://vuejs.org/guide/components/slots.html#render-scope) or React, this is useful if you want to make sure that components don't accidentally access variables defined outside of the component.
|
|
1475
|
+
|
|
1476
|
+
Inside the `{% fill %}` tag, you can ONLY access variables from 2 places:
|
|
1477
|
+
- `get_context_data()` of the component which defined the template (AKA the "root" component)
|
|
1478
|
+
- Any loops (`{% for ... %}`) that the `{% fill %}` tag is part of.
|
|
1460
1479
|
|
|
1461
1480
|
```python
|
|
1462
1481
|
COMPONENTS = {
|
|
@@ -1466,75 +1485,76 @@ COMPONENTS = {
|
|
|
1466
1485
|
|
|
1467
1486
|
#### Example "django"
|
|
1468
1487
|
|
|
1469
|
-
Given this template
|
|
1470
|
-
|
|
1471
|
-
```django
|
|
1472
|
-
{% with cheese="feta" %}
|
|
1473
|
-
{% component 'my_comp' %}
|
|
1474
|
-
{{ my_var }} # my_var
|
|
1475
|
-
{{ cheese }} # cheese
|
|
1476
|
-
{% endcomponent %}
|
|
1477
|
-
{% endwith %}
|
|
1478
|
-
```
|
|
1479
|
-
|
|
1480
|
-
and this context returned from the `get_context_data()` method
|
|
1488
|
+
Given this template:
|
|
1481
1489
|
|
|
1482
1490
|
```py
|
|
1483
|
-
|
|
1491
|
+
class RootComp(component.Component):
|
|
1492
|
+
template = """
|
|
1493
|
+
{% with cheese="feta" %}
|
|
1494
|
+
{% component 'my_comp' %}
|
|
1495
|
+
{{ my_var }} # my_var
|
|
1496
|
+
{{ cheese }} # cheese
|
|
1497
|
+
{% endcomponent %}
|
|
1498
|
+
{% endwith %}
|
|
1499
|
+
"""
|
|
1500
|
+
def get_context_data(self):
|
|
1501
|
+
return { "my_var": 123 }
|
|
1484
1502
|
```
|
|
1485
1503
|
|
|
1486
|
-
Then if component "my_comp"
|
|
1504
|
+
Then if `get_context_data()` of the component `"my_comp"` returns following data:
|
|
1487
1505
|
|
|
1488
1506
|
```py
|
|
1489
1507
|
{ "my_var": 456 }
|
|
1490
1508
|
```
|
|
1491
1509
|
|
|
1492
|
-
Then
|
|
1510
|
+
Then the template will be rendered as:
|
|
1493
1511
|
|
|
1494
1512
|
```django
|
|
1495
1513
|
456 # my_var
|
|
1496
1514
|
feta # cheese
|
|
1497
1515
|
```
|
|
1498
1516
|
|
|
1499
|
-
Because "my_comp"
|
|
1517
|
+
Because `"my_comp"` overshadows the variable `"my_var"`,
|
|
1500
1518
|
so `{{ my_var }}` equals `456`.
|
|
1501
|
-
And variable "cheese" equals `feta`, because the fill CAN access
|
|
1502
|
-
the current context.
|
|
1503
1519
|
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
Given this template
|
|
1520
|
+
And variable `"cheese"` equals `feta`, because the fill CAN access
|
|
1521
|
+
all the data defined in the outer layers, like the `{% with %}` tag.
|
|
1507
1522
|
|
|
1508
|
-
|
|
1509
|
-
{% with cheese="feta" %}
|
|
1510
|
-
{% component 'my_comp' %}
|
|
1511
|
-
{{ my_var }} # my_var
|
|
1512
|
-
{{ cheese }} # cheese
|
|
1513
|
-
{% endcomponent %}
|
|
1514
|
-
{% endwith %}
|
|
1515
|
-
```
|
|
1523
|
+
#### Example "isolated"
|
|
1516
1524
|
|
|
1517
|
-
|
|
1525
|
+
Given this template:
|
|
1518
1526
|
|
|
1519
1527
|
```py
|
|
1520
|
-
|
|
1528
|
+
class RootComp(component.Component):
|
|
1529
|
+
template = """
|
|
1530
|
+
{% with cheese="feta" %}
|
|
1531
|
+
{% component 'my_comp' %}
|
|
1532
|
+
{{ my_var }} # my_var
|
|
1533
|
+
{{ cheese }} # cheese
|
|
1534
|
+
{% endcomponent %}
|
|
1535
|
+
{% endwith %}
|
|
1536
|
+
"""
|
|
1537
|
+
def get_context_data(self):
|
|
1538
|
+
return { "my_var": 123 }
|
|
1521
1539
|
```
|
|
1522
1540
|
|
|
1523
|
-
Then if component "my_comp"
|
|
1541
|
+
Then if `get_context_data()` of the component `"my_comp"` returns following data:
|
|
1524
1542
|
|
|
1525
1543
|
```py
|
|
1526
1544
|
{ "my_var": 456 }
|
|
1527
1545
|
```
|
|
1528
1546
|
|
|
1529
|
-
Then
|
|
1547
|
+
Then the template will be rendered as:
|
|
1530
1548
|
|
|
1531
1549
|
```django
|
|
1532
1550
|
123 # my_var
|
|
1533
1551
|
# cheese
|
|
1534
1552
|
```
|
|
1535
1553
|
|
|
1536
|
-
Because
|
|
1537
|
-
But since "cheese" is not defined there, it's empty.
|
|
1554
|
+
Because variables `"my_var"` and `"cheese"` are searched only inside `RootComponent.get_context_data()`.
|
|
1555
|
+
But since `"cheese"` is not defined there, it's empty.
|
|
1556
|
+
|
|
1557
|
+
Notice that the variables defined with the `{% with %}` tag are ignored inside the `{% fill %}` tag with the `"isolated"` mode.
|
|
1538
1558
|
|
|
1539
1559
|
## Logging and debugging
|
|
1540
1560
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "django_components"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.79"
|
|
8
8
|
requires-python = ">=3.8, <4.0"
|
|
9
9
|
description = "A way to create simple reusable template components in Django."
|
|
10
10
|
keywords = ["django", "components", "css", "js", "html"]
|
|
@@ -105,7 +105,7 @@ class AppSettings:
|
|
|
105
105
|
|
|
106
106
|
@property
|
|
107
107
|
def CONTEXT_BEHAVIOR(self) -> ContextBehavior:
|
|
108
|
-
raw_value = self.settings.get("context_behavior", ContextBehavior.
|
|
108
|
+
raw_value = self.settings.get("context_behavior", ContextBehavior.DJANGO.value)
|
|
109
109
|
return self._validate_context_behavior(raw_value)
|
|
110
110
|
|
|
111
111
|
def _validate_context_behavior(self, raw_value: ContextBehavior) -> ContextBehavior:
|
|
@@ -11,6 +11,9 @@ from django.utils.safestring import SafeString, mark_safe
|
|
|
11
11
|
|
|
12
12
|
from django_components.template_parser import process_aggregate_kwargs
|
|
13
13
|
|
|
14
|
+
HTML_ATTRS_DEFAULTS_KEY = "defaults"
|
|
15
|
+
HTML_ATTRS_ATTRS_KEY = "attrs"
|
|
16
|
+
|
|
14
17
|
|
|
15
18
|
class HtmlAttrsNode(Node):
|
|
16
19
|
def __init__(
|
|
@@ -30,12 +33,12 @@ class HtmlAttrsNode(Node):
|
|
|
30
33
|
# Resolve kwargs, while also extracting attrs and defaults keys
|
|
31
34
|
for key, value in self.kwargs:
|
|
32
35
|
resolved_value = value.resolve(context)
|
|
33
|
-
if key.startswith("
|
|
36
|
+
if key.startswith(f"{HTML_ATTRS_ATTRS_KEY}:") or key.startswith(f"{HTML_ATTRS_DEFAULTS_KEY}:"):
|
|
34
37
|
attrs_and_defaults_from_kwargs[key] = resolved_value
|
|
35
38
|
continue
|
|
36
39
|
# NOTE: These were already extracted into separate variables, so
|
|
37
40
|
# ignore them here.
|
|
38
|
-
elif key ==
|
|
41
|
+
elif key == HTML_ATTRS_ATTRS_KEY or key == HTML_ATTRS_DEFAULTS_KEY:
|
|
39
42
|
continue
|
|
40
43
|
|
|
41
44
|
append_attrs.append((key, resolved_value))
|
|
@@ -46,16 +49,16 @@ class HtmlAttrsNode(Node):
|
|
|
46
49
|
# So by assigning the `attrs` and `defaults` keys, users are forced to use only
|
|
47
50
|
# one approach or the other, but not both simultaneously.
|
|
48
51
|
if self.attributes:
|
|
49
|
-
attrs_and_defaults_from_kwargs[
|
|
52
|
+
attrs_and_defaults_from_kwargs[HTML_ATTRS_ATTRS_KEY] = self.attributes.resolve(context)
|
|
50
53
|
if self.default_attrs:
|
|
51
|
-
attrs_and_defaults_from_kwargs[
|
|
54
|
+
attrs_and_defaults_from_kwargs[HTML_ATTRS_DEFAULTS_KEY] = self.default_attrs.resolve(context)
|
|
52
55
|
|
|
53
56
|
# Turn `{"attrs:blabla": 1}` into `{"attrs": {"blabla": 1}}`
|
|
54
57
|
attrs_and_defaults_from_kwargs = process_aggregate_kwargs(attrs_and_defaults_from_kwargs)
|
|
55
58
|
|
|
56
59
|
# NOTE: We want to allow to use `html_attrs` even without `attrs` or `defaults` params
|
|
57
|
-
attrs = attrs_and_defaults_from_kwargs.get(
|
|
58
|
-
default_attrs = attrs_and_defaults_from_kwargs.get(
|
|
60
|
+
attrs = attrs_and_defaults_from_kwargs.get(HTML_ATTRS_ATTRS_KEY, {})
|
|
61
|
+
default_attrs = attrs_and_defaults_from_kwargs.get(HTML_ATTRS_DEFAULTS_KEY, {})
|
|
59
62
|
|
|
60
63
|
final_attrs = {**default_attrs, **attrs}
|
|
61
64
|
final_attrs = append_attributes(*final_attrs.items(), *append_attrs)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import inspect
|
|
2
2
|
import os
|
|
3
3
|
import sys
|
|
4
|
+
import types
|
|
4
5
|
from pathlib import Path
|
|
5
6
|
from typing import Any, ClassVar, Dict, List, Mapping, MutableMapping, Optional, Tuple, Type, Union
|
|
6
7
|
|
|
@@ -11,6 +12,7 @@ from django.template.base import FilterExpression, Node, NodeList, Template, Tex
|
|
|
11
12
|
from django.template.context import Context
|
|
12
13
|
from django.template.exceptions import TemplateSyntaxError
|
|
13
14
|
from django.template.loader import get_template
|
|
15
|
+
from django.template.loader_tags import BLOCK_CONTEXT_KEY
|
|
14
16
|
from django.utils.html import escape
|
|
15
17
|
from django.utils.safestring import SafeString, mark_safe
|
|
16
18
|
from django.views import View
|
|
@@ -285,7 +287,14 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
285
287
|
# See https://github.com/EmilStenstrom/django-components/issues/414
|
|
286
288
|
context = context if isinstance(context, Context) else Context(context)
|
|
287
289
|
prepare_context(context, self.component_id)
|
|
290
|
+
|
|
288
291
|
template = self.get_template(context)
|
|
292
|
+
_monkeypatch_template(template)
|
|
293
|
+
|
|
294
|
+
# Set `Template._is_component_nested` based on whether we're currently INSIDE
|
|
295
|
+
# the `{% extends %}` tag.
|
|
296
|
+
# Part of fix for https://github.com/EmilStenstrom/django-components/issues/508
|
|
297
|
+
template._is_component_nested = bool(context.render_context.get(BLOCK_CONTEXT_KEY))
|
|
289
298
|
|
|
290
299
|
# Support passing slots explicitly to `render` method
|
|
291
300
|
if slots_data:
|
|
@@ -445,3 +454,47 @@ class ComponentNode(Node):
|
|
|
445
454
|
|
|
446
455
|
trace_msg("RENDR", "COMP", self.name_fexp, self.component_id, "...Done!")
|
|
447
456
|
return output
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
def _monkeypatch_template(template: Template) -> None:
|
|
460
|
+
# Modify `Template.render` to set `isolated_context` kwarg of `push_state`
|
|
461
|
+
# based on our custom `Template._is_component_nested`.
|
|
462
|
+
#
|
|
463
|
+
# Part of fix for https://github.com/EmilStenstrom/django-components/issues/508
|
|
464
|
+
#
|
|
465
|
+
# NOTE 1: While we could've subclassed Template, then we would need to either
|
|
466
|
+
# 1) ask the user to change the backend, so all templates are of our subclass, or
|
|
467
|
+
# 2) copy the data from user's Template class instance to our subclass instance,
|
|
468
|
+
# which could lead to doubly parsing the source, and could be problematic if users
|
|
469
|
+
# used more exotic subclasses of Template.
|
|
470
|
+
#
|
|
471
|
+
# Instead, modifying only the `render` method of an already-existing instance
|
|
472
|
+
# should work well with any user-provided custom subclasses of Template, and it
|
|
473
|
+
# doesn't require the source to be parsed multiple times. User can pass extra args/kwargs,
|
|
474
|
+
# and can modify the rendering behavior by overriding the `_render` method.
|
|
475
|
+
#
|
|
476
|
+
# NOTE 2: Instead of setting `Template._is_component_nested`, alternatively we could
|
|
477
|
+
# have passed the value to `_monkeypatch_template` directly. However, we intentionally
|
|
478
|
+
# did NOT do that, so the monkey-patched method is more robust, and can be e.g. copied
|
|
479
|
+
# to other.
|
|
480
|
+
def _template_render(self: Template, context: Context, *args: Any, **kwargs: Any) -> str:
|
|
481
|
+
# ---------------- OUR CHANGES START ----------------
|
|
482
|
+
# We parametrized `isolated_context`, which was `True` in the original method.
|
|
483
|
+
if not hasattr(self, "_is_component_nested"):
|
|
484
|
+
isolated_context = True
|
|
485
|
+
else:
|
|
486
|
+
# MUST be `True` for templates that are NOT import with `{% extends %}` tag,
|
|
487
|
+
# and `False` otherwise.
|
|
488
|
+
isolated_context = not self._is_component_nested
|
|
489
|
+
# ---------------- OUR CHANGES END ----------------
|
|
490
|
+
|
|
491
|
+
with context.render_context.push_state(self, isolated_context=isolated_context):
|
|
492
|
+
if context.template is None:
|
|
493
|
+
with context.bind_template(self):
|
|
494
|
+
context.template_name = self.name
|
|
495
|
+
return self._render(context, *args, **kwargs)
|
|
496
|
+
else:
|
|
497
|
+
return self._render(context, *args, **kwargs)
|
|
498
|
+
|
|
499
|
+
# See https://stackoverflow.com/a/42154067/9788634
|
|
500
|
+
template.render = types.MethodType(_template_render, template)
|