django-components 0.76__tar.gz → 0.78__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.76/src/django_components.egg-info → django_components-0.78}/PKG-INFO +25 -6
- {django_components-0.76 → django_components-0.78}/README.md +24 -5
- {django_components-0.76 → django_components-0.78}/pyproject.toml +1 -1
- {django_components-0.76 → django_components-0.78}/src/django_components/attributes.py +9 -6
- {django_components-0.76 → django_components-0.78}/src/django_components/component.py +59 -6
- {django_components-0.76 → django_components-0.78}/src/django_components/slots.py +46 -39
- {django_components-0.76 → django_components-0.78}/src/django_components/templatetags/component_tags.py +25 -18
- {django_components-0.76 → django_components-0.78/src/django_components.egg-info}/PKG-INFO +25 -6
- {django_components-0.76 → django_components-0.78}/src/django_components.egg-info/SOURCES.txt +5 -1
- {django_components-0.76 → django_components-0.78}/tests/test_attributes.py +3 -3
- django_components-0.78/tests/test_component.py +128 -0
- {django_components-0.76 → django_components-0.78}/tests/test_component_as_view.py +25 -1
- django_components-0.78/tests/test_component_media.py +388 -0
- {django_components-0.76 → django_components-0.78}/tests/test_context.py +257 -127
- {django_components-0.76 → django_components-0.78}/tests/test_dependency_rendering.py +3 -1
- {django_components-0.76 → django_components-0.78}/tests/test_template_parser.py +17 -15
- django_components-0.78/tests/test_templatetags.py +455 -0
- django_components-0.78/tests/test_templatetags_component.py +417 -0
- django_components-0.78/tests/test_templatetags_slot_fill.py +1294 -0
- django_components-0.78/tests/test_templatetags_templating.py +826 -0
- django_components-0.78/tests/testutils.py +176 -0
- django_components-0.76/tests/test_component.py +0 -1021
- django_components-0.76/tests/test_templatetags.py +0 -2605
- django_components-0.76/tests/testutils.py +0 -77
- {django_components-0.76 → django_components-0.78}/LICENSE +0 -0
- {django_components-0.76 → django_components-0.78}/setup.cfg +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/__init__.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/app_settings.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/apps.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/component_registry.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/context.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/expression.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/logger.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/management/__init__.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/management/commands/__init__.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/management/commands/startcomponent.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/management/commands/upgradecomponent.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/middleware.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/node.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/py.typed +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/safer_staticfiles/__init__.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/safer_staticfiles/apps.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/template_loader.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/template_parser.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/templatetags/__init__.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/types.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components/utils.py +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components.egg-info/requires.txt +0 -0
- {django_components-0.76 → django_components-0.78}/src/django_components.egg-info/top_level.txt +0 -0
- {django_components-0.76 → django_components-0.78}/tests/test_autodiscover.py +0 -0
- /django_components-0.76/tests/test_startcomponent_command.py → /django_components-0.78/tests/test_command_startcomponent.py +0 -0
- {django_components-0.76 → django_components-0.78}/tests/test_registry.py +0 -0
- {django_components-0.76 → django_components-0.78}/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.78
|
|
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,12 @@ Read on to learn about the details!
|
|
|
68
68
|
|
|
69
69
|
## Release notes
|
|
70
70
|
|
|
71
|
+
🚨📢 **Version 0.77** CHANGED the syntax for accessing default slot content.
|
|
72
|
+
- Previously, the syntax was
|
|
73
|
+
`{% fill "my_slot" as "alias" %}` and `{{ alias.default }}`.
|
|
74
|
+
- Now, the syntax is
|
|
75
|
+
`{% fill "my_slot" default="alias" %}` and `{{ alias }}`.
|
|
76
|
+
|
|
71
77
|
**Version 0.74** introduces `html_attrs` tag and `prefix:key=val` construct for passing dicts to components.
|
|
72
78
|
|
|
73
79
|
🚨📢 **Version 0.70**
|
|
@@ -651,13 +657,26 @@ Which you can then use are regular default slot:
|
|
|
651
657
|
|
|
652
658
|
_Added in version 0.26_
|
|
653
659
|
|
|
654
|
-
|
|
660
|
+
> NOTE: In version 0.77, the syntax was changed from
|
|
661
|
+
> ```django
|
|
662
|
+
> {% fill "my_slot" as "alias" %} {{ alias.default }}
|
|
663
|
+
> ```
|
|
664
|
+
> to
|
|
665
|
+
> ```django
|
|
666
|
+
> {% fill "my_slot" default="slot_default" %} {{ slot_default }}
|
|
667
|
+
> ```
|
|
668
|
+
|
|
669
|
+
Sometimes you may want to keep the original slot, but only wrap or prepend/append content to it. To do so, you can access the default slot via the `default` kwarg.
|
|
670
|
+
|
|
671
|
+
Similarly to the `data` attribute, you specify the variable name through which the default slot will be made available.
|
|
655
672
|
|
|
656
|
-
|
|
673
|
+
For instance, let's say you're filling a slot called 'body'. To render the original slot, assign it to a variable using the `'default'` keyword. You then render this variable to insert the default content:
|
|
657
674
|
|
|
658
675
|
```htmldjango
|
|
659
676
|
{% component "calendar" date="2020-06-06" %}
|
|
660
|
-
{% fill "body"
|
|
677
|
+
{% fill "body" default="body_default" %}
|
|
678
|
+
{{ body_default }}. Have a great day!
|
|
679
|
+
{% endfill %}
|
|
661
680
|
{% endcomponent %}
|
|
662
681
|
```
|
|
663
682
|
|
|
@@ -856,12 +875,12 @@ While this does not:
|
|
|
856
875
|
```
|
|
857
876
|
|
|
858
877
|
Note: You cannot set the `data` attribute and
|
|
859
|
-
[
|
|
878
|
+
[`default` attribute)](#accessing-original-content-of-slots)
|
|
860
879
|
to the same name. This raises an error:
|
|
861
880
|
|
|
862
881
|
```django
|
|
863
882
|
{% component "my_comp" %}
|
|
864
|
-
{% fill "content" data="slot_var"
|
|
883
|
+
{% fill "content" data="slot_var" default="slot_var" %}
|
|
865
884
|
{{ slot_var.input }}
|
|
866
885
|
{% endfill %}
|
|
867
886
|
{% endcomponent %}
|
|
@@ -44,6 +44,12 @@ Read on to learn about the details!
|
|
|
44
44
|
|
|
45
45
|
## Release notes
|
|
46
46
|
|
|
47
|
+
🚨📢 **Version 0.77** CHANGED the syntax for accessing default slot content.
|
|
48
|
+
- Previously, the syntax was
|
|
49
|
+
`{% fill "my_slot" as "alias" %}` and `{{ alias.default }}`.
|
|
50
|
+
- Now, the syntax is
|
|
51
|
+
`{% fill "my_slot" default="alias" %}` and `{{ alias }}`.
|
|
52
|
+
|
|
47
53
|
**Version 0.74** introduces `html_attrs` tag and `prefix:key=val` construct for passing dicts to components.
|
|
48
54
|
|
|
49
55
|
🚨📢 **Version 0.70**
|
|
@@ -627,13 +633,26 @@ Which you can then use are regular default slot:
|
|
|
627
633
|
|
|
628
634
|
_Added in version 0.26_
|
|
629
635
|
|
|
630
|
-
|
|
636
|
+
> NOTE: In version 0.77, the syntax was changed from
|
|
637
|
+
> ```django
|
|
638
|
+
> {% fill "my_slot" as "alias" %} {{ alias.default }}
|
|
639
|
+
> ```
|
|
640
|
+
> to
|
|
641
|
+
> ```django
|
|
642
|
+
> {% fill "my_slot" default="slot_default" %} {{ slot_default }}
|
|
643
|
+
> ```
|
|
644
|
+
|
|
645
|
+
Sometimes you may want to keep the original slot, but only wrap or prepend/append content to it. To do so, you can access the default slot via the `default` kwarg.
|
|
646
|
+
|
|
647
|
+
Similarly to the `data` attribute, you specify the variable name through which the default slot will be made available.
|
|
631
648
|
|
|
632
|
-
|
|
649
|
+
For instance, let's say you're filling a slot called 'body'. To render the original slot, assign it to a variable using the `'default'` keyword. You then render this variable to insert the default content:
|
|
633
650
|
|
|
634
651
|
```htmldjango
|
|
635
652
|
{% component "calendar" date="2020-06-06" %}
|
|
636
|
-
{% fill "body"
|
|
653
|
+
{% fill "body" default="body_default" %}
|
|
654
|
+
{{ body_default }}. Have a great day!
|
|
655
|
+
{% endfill %}
|
|
637
656
|
{% endcomponent %}
|
|
638
657
|
```
|
|
639
658
|
|
|
@@ -832,12 +851,12 @@ While this does not:
|
|
|
832
851
|
```
|
|
833
852
|
|
|
834
853
|
Note: You cannot set the `data` attribute and
|
|
835
|
-
[
|
|
854
|
+
[`default` attribute)](#accessing-original-content-of-slots)
|
|
836
855
|
to the same name. This raises an error:
|
|
837
856
|
|
|
838
857
|
```django
|
|
839
858
|
{% component "my_comp" %}
|
|
840
|
-
{% fill "content" data="slot_var"
|
|
859
|
+
{% fill "content" data="slot_var" default="slot_var" %}
|
|
841
860
|
{{ slot_var.input }}
|
|
842
861
|
{% endfill %}
|
|
843
862
|
{% endcomponent %}
|
|
@@ -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.78"
|
|
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"]
|
|
@@ -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:
|
|
@@ -358,8 +367,8 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
358
367
|
slot_fills = {
|
|
359
368
|
slot_name: FillContent(
|
|
360
369
|
nodes=NodeList([TextNode(escape(content) if escape_content else content)]),
|
|
361
|
-
|
|
362
|
-
|
|
370
|
+
slot_default_var=None,
|
|
371
|
+
slot_data_var=None,
|
|
363
372
|
)
|
|
364
373
|
for (slot_name, content) in slots_data.items()
|
|
365
374
|
}
|
|
@@ -422,12 +431,12 @@ class ComponentNode(Node):
|
|
|
422
431
|
f"Detected duplicate fill tag name '{resolved_name}'."
|
|
423
432
|
)
|
|
424
433
|
|
|
425
|
-
|
|
426
|
-
|
|
434
|
+
resolved_slot_default_var = fill_node.resolve_slot_default(context, resolved_component_name)
|
|
435
|
+
resolved_slot_data_var = fill_node.resolve_slot_data(context, resolved_component_name)
|
|
427
436
|
fill_content[resolved_name] = FillContent(
|
|
428
437
|
nodes=fill_node.nodelist,
|
|
429
|
-
|
|
430
|
-
|
|
438
|
+
slot_default_var=resolved_slot_default_var,
|
|
439
|
+
slot_data_var=resolved_slot_data_var,
|
|
431
440
|
)
|
|
432
441
|
|
|
433
442
|
component: Component = component_cls(
|
|
@@ -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)
|
|
@@ -24,8 +24,8 @@ DEFAULT_SLOT_KEY = "_DJANGO_COMPONENTS_DEFAULT_SLOT"
|
|
|
24
24
|
|
|
25
25
|
SlotId = str
|
|
26
26
|
SlotName = str
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
SlotDefaultName = str
|
|
28
|
+
SlotDataName = str
|
|
29
29
|
|
|
30
30
|
|
|
31
31
|
class FillContent(NamedTuple):
|
|
@@ -44,8 +44,8 @@ class FillContent(NamedTuple):
|
|
|
44
44
|
"""
|
|
45
45
|
|
|
46
46
|
nodes: NodeList
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
slot_default_var: Optional[SlotDefaultName]
|
|
48
|
+
slot_data_var: Optional[SlotDataName]
|
|
49
49
|
|
|
50
50
|
|
|
51
51
|
class Slot(NamedTuple):
|
|
@@ -80,27 +80,26 @@ class SlotFill(NamedTuple):
|
|
|
80
80
|
is_filled: bool
|
|
81
81
|
nodelist: NodeList
|
|
82
82
|
context_data: Dict
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
slot_default_var: Optional[SlotDefaultName]
|
|
84
|
+
slot_data_var: Optional[SlotDataName]
|
|
85
85
|
|
|
86
86
|
|
|
87
|
-
class
|
|
87
|
+
class LazySlot:
|
|
88
88
|
"""
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
Bound a slot and a context, so we can move these around as a single variable,
|
|
90
|
+
and lazily render the slot with given context only once coerced to string.
|
|
91
91
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
SlotNode.render() for implementation).
|
|
92
|
+
This is used to access slots as variables inside the templates. When a LazySlot
|
|
93
|
+
is rendered in the template with `{{ my_lazy_slot }}`, it will output the contents
|
|
94
|
+
of the slot.
|
|
96
95
|
"""
|
|
97
96
|
|
|
98
97
|
def __init__(self, slot: "SlotNode", context: Context):
|
|
99
98
|
self._slot = slot
|
|
100
99
|
self._context = context
|
|
101
100
|
|
|
102
|
-
|
|
103
|
-
def
|
|
101
|
+
# Render the slot when the template coerces LazySlot to string
|
|
102
|
+
def __str__(self) -> str:
|
|
104
103
|
return mark_safe(self._slot.nodelist.render(self._context))
|
|
105
104
|
|
|
106
105
|
|
|
@@ -141,20 +140,28 @@ class SlotNode(Node):
|
|
|
141
140
|
|
|
142
141
|
extra_context: Dict[str, Any] = {}
|
|
143
142
|
|
|
144
|
-
# If slot is using
|
|
145
|
-
# the context, so users can refer to the slot from within the
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
143
|
+
# If slot fill is using `{% fill "myslot" default="abc" %}`, then set the "abc" to
|
|
144
|
+
# the context, so users can refer to the default slot from within the fill content.
|
|
145
|
+
default_var = slot_fill.slot_default_var
|
|
146
|
+
if default_var:
|
|
147
|
+
if not default_var.isidentifier():
|
|
148
|
+
raise TemplateSyntaxError(
|
|
149
|
+
f"Slot default alias in fill '{self.name}' must be a valid identifier. Got '{default_var}'"
|
|
150
|
+
)
|
|
151
|
+
extra_context[default_var] = LazySlot(self, context)
|
|
150
152
|
|
|
151
153
|
# Expose the kwargs that were passed to the `{% slot %}` tag. These kwargs
|
|
152
154
|
# are made available through a variable name that was set on the `{% fill %}`
|
|
153
155
|
# tag.
|
|
154
|
-
|
|
156
|
+
data_var = slot_fill.slot_data_var
|
|
157
|
+
if data_var:
|
|
158
|
+
if not data_var.isidentifier():
|
|
159
|
+
raise TemplateSyntaxError(
|
|
160
|
+
f"Slot data alias in fill '{self.name}' must be a valid identifier. Got '{data_var}'"
|
|
161
|
+
)
|
|
155
162
|
slot_kwargs = safe_resolve_dict(self.slot_kwargs, context)
|
|
156
163
|
slot_kwargs = process_aggregate_kwargs(slot_kwargs)
|
|
157
|
-
extra_context[
|
|
164
|
+
extra_context[data_var] = slot_kwargs
|
|
158
165
|
|
|
159
166
|
# For the user-provided slot fill, we want to use the context of where the slot
|
|
160
167
|
# came from (or current context if configured so)
|
|
@@ -192,17 +199,17 @@ class FillNode(Node):
|
|
|
192
199
|
self,
|
|
193
200
|
nodelist: NodeList,
|
|
194
201
|
name_fexp: FilterExpression,
|
|
195
|
-
|
|
196
|
-
|
|
202
|
+
slot_default_var_fexp: Optional[FilterExpression] = None,
|
|
203
|
+
slot_data_var_fexp: Optional[FilterExpression] = None,
|
|
197
204
|
is_implicit: bool = False,
|
|
198
205
|
node_id: Optional[str] = None,
|
|
199
206
|
):
|
|
200
207
|
self.node_id = node_id or gen_id()
|
|
201
208
|
self.nodelist = nodelist
|
|
202
209
|
self.name_fexp = name_fexp
|
|
203
|
-
self.
|
|
210
|
+
self.slot_default_var_fexp = slot_default_var_fexp
|
|
204
211
|
self.is_implicit = is_implicit
|
|
205
|
-
self.
|
|
212
|
+
self.slot_data_var_fexp = slot_data_var_fexp
|
|
206
213
|
self.component_id: Optional[str] = None
|
|
207
214
|
|
|
208
215
|
def render(self, context: Context) -> str:
|
|
@@ -215,11 +222,11 @@ class FillNode(Node):
|
|
|
215
222
|
def __repr__(self) -> str:
|
|
216
223
|
return f"<{type(self)} Name: {self.name_fexp}. Contents: {repr(self.nodelist)}.>"
|
|
217
224
|
|
|
218
|
-
def
|
|
219
|
-
return self.resolve_fexp("
|
|
225
|
+
def resolve_slot_default(self, context: Context, component_name: Optional[str] = None) -> Optional[str]:
|
|
226
|
+
return self.resolve_fexp("slot default", self.slot_default_var_fexp, context, component_name)
|
|
220
227
|
|
|
221
|
-
def
|
|
222
|
-
return self.resolve_fexp("
|
|
228
|
+
def resolve_slot_data(self, context: Context, component_name: Optional[str] = None) -> Optional[str]:
|
|
229
|
+
return self.resolve_fexp("slot data", self.slot_data_var_fexp, context, component_name)
|
|
223
230
|
|
|
224
231
|
def resolve_fexp(
|
|
225
232
|
self,
|
|
@@ -232,14 +239,14 @@ class FillNode(Node):
|
|
|
232
239
|
return None
|
|
233
240
|
|
|
234
241
|
try:
|
|
235
|
-
|
|
242
|
+
resolved_name = resolve_expression_as_identifier(context, fexp)
|
|
236
243
|
except ValueError as err:
|
|
237
244
|
raise TemplateSyntaxError(
|
|
238
245
|
f"Fill tag {name} '{fexp.var}' in component {component_name}"
|
|
239
246
|
f"does not resolve to a valid Python identifier."
|
|
240
247
|
) from err
|
|
241
248
|
|
|
242
|
-
return
|
|
249
|
+
return resolved_name
|
|
243
250
|
|
|
244
251
|
|
|
245
252
|
def parse_slot_fill_nodes_from_component_nodelist(
|
|
@@ -363,8 +370,8 @@ def resolve_slots(
|
|
|
363
370
|
is_filled=True,
|
|
364
371
|
nodelist=fill.nodes,
|
|
365
372
|
context_data=context_data,
|
|
366
|
-
|
|
367
|
-
|
|
373
|
+
slot_default_var=fill.slot_default_var,
|
|
374
|
+
slot_data_var=fill.slot_data_var,
|
|
368
375
|
)
|
|
369
376
|
for name, fill in fill_content.items()
|
|
370
377
|
}
|
|
@@ -452,8 +459,8 @@ def resolve_slots(
|
|
|
452
459
|
is_filled=False,
|
|
453
460
|
nodelist=slot.nodelist,
|
|
454
461
|
context_data=context_data,
|
|
455
|
-
|
|
456
|
-
|
|
462
|
+
slot_default_var=None,
|
|
463
|
+
slot_data_var=None,
|
|
457
464
|
)
|
|
458
465
|
# Since the slot's default CAN include other slots (because it's defined in
|
|
459
466
|
# the same template), we need to enqueue the slot's children
|
|
@@ -500,8 +507,8 @@ def _resolve_default_slot(
|
|
|
500
507
|
is_filled=default_fill.is_filled,
|
|
501
508
|
nodelist=default_fill.nodelist,
|
|
502
509
|
context_data=default_fill.context_data,
|
|
503
|
-
|
|
504
|
-
|
|
510
|
+
slot_default_var=default_fill.slot_default_var,
|
|
511
|
+
slot_data_var=default_fill.slot_data_var,
|
|
505
512
|
# Updated fields
|
|
506
513
|
name=slot.name,
|
|
507
514
|
escaped_name=_escape_slot_name(slot.name),
|
|
@@ -31,6 +31,7 @@ register = django.template.Library()
|
|
|
31
31
|
SLOT_REQUIRED_OPTION_KEYWORD = "required"
|
|
32
32
|
SLOT_DEFAULT_OPTION_KEYWORD = "default"
|
|
33
33
|
SLOT_DATA_ATTR = "data"
|
|
34
|
+
SLOT_DEFAULT_ATTR = "default"
|
|
34
35
|
|
|
35
36
|
|
|
36
37
|
def get_components_from_registry(registry: ComponentRegistry) -> List["Component"]:
|
|
@@ -147,7 +148,7 @@ def do_fill(parser: Parser, token: Token) -> FillNode:
|
|
|
147
148
|
"""
|
|
148
149
|
# e.g. {% fill <name> %}
|
|
149
150
|
tag_name, *args = token.split_contents()
|
|
150
|
-
slot_name_fexp,
|
|
151
|
+
slot_name_fexp, slot_default_var_fexp, slot_data_var_fexp = _parse_fill_args(parser, args, tag_name)
|
|
151
152
|
|
|
152
153
|
# Use a unique ID to be able to tie the fill nodes with components and slots
|
|
153
154
|
# NOTE: MUST be called BEFORE `parser.parse()` to ensure predictable numbering
|
|
@@ -160,8 +161,8 @@ def do_fill(parser: Parser, token: Token) -> FillNode:
|
|
|
160
161
|
fill_node = FillNode(
|
|
161
162
|
nodelist,
|
|
162
163
|
name_fexp=slot_name_fexp,
|
|
163
|
-
|
|
164
|
-
|
|
164
|
+
slot_default_var_fexp=slot_default_var_fexp,
|
|
165
|
+
slot_data_var_fexp=slot_data_var_fexp,
|
|
165
166
|
node_id=fill_id,
|
|
166
167
|
)
|
|
167
168
|
|
|
@@ -382,19 +383,13 @@ def _parse_fill_args(
|
|
|
382
383
|
) -> Tuple[FilterExpression, Optional[FilterExpression], Optional[FilterExpression]]:
|
|
383
384
|
if not len(bits):
|
|
384
385
|
raise TemplateSyntaxError(
|
|
385
|
-
"'fill' tag does not match pattern "
|
|
386
|
+
"'fill' tag does not match pattern "
|
|
387
|
+
f"{{% fill <name> [{SLOT_DATA_ATTR}=val] [{SLOT_DEFAULT_ATTR}=slot_var] %}} "
|
|
386
388
|
)
|
|
387
389
|
|
|
388
390
|
slot_name = bits.pop(0)
|
|
389
391
|
slot_name_fexp = parser.compile_filter(slot_name)
|
|
390
392
|
|
|
391
|
-
alias_fexp: Optional[FilterExpression] = None
|
|
392
|
-
# e.g. {% fill <name> as <alias> %}
|
|
393
|
-
if len(bits) >= 2 and bits[-2].lower() == "as":
|
|
394
|
-
alias = bits.pop()
|
|
395
|
-
bits.pop() # Remove the "as" keyword
|
|
396
|
-
alias_fexp = parser.compile_filter(alias)
|
|
397
|
-
|
|
398
393
|
# Even tho we want to parse only single kwarg, we use the same logic for parsing
|
|
399
394
|
# as we use for other tags, for consistency.
|
|
400
395
|
_, tag_kwarg_pairs = parse_bits(
|
|
@@ -410,17 +405,29 @@ def _parse_fill_args(
|
|
|
410
405
|
raise TemplateSyntaxError(f"'{tag_name}' received multiple values for keyword argument '{key}'")
|
|
411
406
|
tag_kwargs[key] = val
|
|
412
407
|
|
|
413
|
-
|
|
408
|
+
# Extract known kwargs
|
|
409
|
+
slot_data_var_fexp: Optional[FilterExpression] = None
|
|
414
410
|
if SLOT_DATA_ATTR in tag_kwargs:
|
|
415
|
-
|
|
416
|
-
if not is_wrapped_in_quotes(
|
|
411
|
+
slot_data_var_fexp = tag_kwargs.pop(SLOT_DATA_ATTR)
|
|
412
|
+
if not is_wrapped_in_quotes(slot_data_var_fexp.token):
|
|
413
|
+
raise TemplateSyntaxError(
|
|
414
|
+
f"Value of '{SLOT_DATA_ATTR}' in '{tag_name}' tag must be a string literal, got '{slot_data_var_fexp}'"
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
slot_default_var_fexp: Optional[FilterExpression] = None
|
|
418
|
+
if SLOT_DEFAULT_ATTR in tag_kwargs:
|
|
419
|
+
slot_default_var_fexp = tag_kwargs.pop(SLOT_DEFAULT_ATTR)
|
|
420
|
+
if not is_wrapped_in_quotes(slot_default_var_fexp.token):
|
|
417
421
|
raise TemplateSyntaxError(
|
|
418
|
-
f"Value of '{
|
|
422
|
+
f"Value of '{SLOT_DEFAULT_ATTR}' in '{tag_name}' tag must be a string literal,"
|
|
423
|
+
f" got '{slot_default_var_fexp}'"
|
|
419
424
|
)
|
|
420
425
|
|
|
421
|
-
|
|
426
|
+
# data and default cannot be bound to the same variable
|
|
427
|
+
if slot_data_var_fexp and slot_default_var_fexp and slot_data_var_fexp.token == slot_default_var_fexp.token:
|
|
422
428
|
raise TemplateSyntaxError(
|
|
423
|
-
f"'{tag_name}' received the same string for slot
|
|
429
|
+
f"'{tag_name}' received the same string for slot default ({SLOT_DEFAULT_ATTR}=...)"
|
|
430
|
+
f" and slot data ({SLOT_DATA_ATTR}=...)"
|
|
424
431
|
)
|
|
425
432
|
|
|
426
433
|
if len(tag_kwargs):
|
|
@@ -428,7 +435,7 @@ def _parse_fill_args(
|
|
|
428
435
|
extra_keys = ", ".join(extra_keywords)
|
|
429
436
|
raise TemplateSyntaxError(f"'{tag_name}' received unexpected kwargs: {extra_keys}")
|
|
430
437
|
|
|
431
|
-
return slot_name_fexp,
|
|
438
|
+
return slot_name_fexp, slot_default_var_fexp, slot_data_var_fexp
|
|
432
439
|
|
|
433
440
|
|
|
434
441
|
def _get_positional_param(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.78
|
|
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,12 @@ Read on to learn about the details!
|
|
|
68
68
|
|
|
69
69
|
## Release notes
|
|
70
70
|
|
|
71
|
+
🚨📢 **Version 0.77** CHANGED the syntax for accessing default slot content.
|
|
72
|
+
- Previously, the syntax was
|
|
73
|
+
`{% fill "my_slot" as "alias" %}` and `{{ alias.default }}`.
|
|
74
|
+
- Now, the syntax is
|
|
75
|
+
`{% fill "my_slot" default="alias" %}` and `{{ alias }}`.
|
|
76
|
+
|
|
71
77
|
**Version 0.74** introduces `html_attrs` tag and `prefix:key=val` construct for passing dicts to components.
|
|
72
78
|
|
|
73
79
|
🚨📢 **Version 0.70**
|
|
@@ -651,13 +657,26 @@ Which you can then use are regular default slot:
|
|
|
651
657
|
|
|
652
658
|
_Added in version 0.26_
|
|
653
659
|
|
|
654
|
-
|
|
660
|
+
> NOTE: In version 0.77, the syntax was changed from
|
|
661
|
+
> ```django
|
|
662
|
+
> {% fill "my_slot" as "alias" %} {{ alias.default }}
|
|
663
|
+
> ```
|
|
664
|
+
> to
|
|
665
|
+
> ```django
|
|
666
|
+
> {% fill "my_slot" default="slot_default" %} {{ slot_default }}
|
|
667
|
+
> ```
|
|
668
|
+
|
|
669
|
+
Sometimes you may want to keep the original slot, but only wrap or prepend/append content to it. To do so, you can access the default slot via the `default` kwarg.
|
|
670
|
+
|
|
671
|
+
Similarly to the `data` attribute, you specify the variable name through which the default slot will be made available.
|
|
655
672
|
|
|
656
|
-
|
|
673
|
+
For instance, let's say you're filling a slot called 'body'. To render the original slot, assign it to a variable using the `'default'` keyword. You then render this variable to insert the default content:
|
|
657
674
|
|
|
658
675
|
```htmldjango
|
|
659
676
|
{% component "calendar" date="2020-06-06" %}
|
|
660
|
-
{% fill "body"
|
|
677
|
+
{% fill "body" default="body_default" %}
|
|
678
|
+
{{ body_default }}. Have a great day!
|
|
679
|
+
{% endfill %}
|
|
661
680
|
{% endcomponent %}
|
|
662
681
|
```
|
|
663
682
|
|
|
@@ -856,12 +875,12 @@ While this does not:
|
|
|
856
875
|
```
|
|
857
876
|
|
|
858
877
|
Note: You cannot set the `data` attribute and
|
|
859
|
-
[
|
|
878
|
+
[`default` attribute)](#accessing-original-content-of-slots)
|
|
860
879
|
to the same name. This raises an error:
|
|
861
880
|
|
|
862
881
|
```django
|
|
863
882
|
{% component "my_comp" %}
|
|
864
|
-
{% fill "content" data="slot_var"
|
|
883
|
+
{% fill "content" data="slot_var" default="slot_var" %}
|
|
865
884
|
{{ slot_var.input }}
|
|
866
885
|
{% endfill %}
|
|
867
886
|
{% endcomponent %}
|
{django_components-0.76 → django_components-0.78}/src/django_components.egg-info/SOURCES.txt
RENAMED
|
@@ -33,13 +33,17 @@ src/django_components/templatetags/__init__.py
|
|
|
33
33
|
src/django_components/templatetags/component_tags.py
|
|
34
34
|
tests/test_attributes.py
|
|
35
35
|
tests/test_autodiscover.py
|
|
36
|
+
tests/test_command_startcomponent.py
|
|
36
37
|
tests/test_component.py
|
|
37
38
|
tests/test_component_as_view.py
|
|
39
|
+
tests/test_component_media.py
|
|
38
40
|
tests/test_context.py
|
|
39
41
|
tests/test_dependency_rendering.py
|
|
40
42
|
tests/test_registry.py
|
|
41
43
|
tests/test_settings.py
|
|
42
|
-
tests/test_startcomponent_command.py
|
|
43
44
|
tests/test_template_parser.py
|
|
44
45
|
tests/test_templatetags.py
|
|
46
|
+
tests/test_templatetags_component.py
|
|
47
|
+
tests/test_templatetags_slot_fill.py
|
|
48
|
+
tests/test_templatetags_templating.py
|
|
45
49
|
tests/testutils.py
|