django-components 0.76__tar.gz → 0.77__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.
Files changed (47) hide show
  1. {django_components-0.76/src/django_components.egg-info → django_components-0.77}/PKG-INFO +25 -6
  2. {django_components-0.76 → django_components-0.77}/README.md +24 -5
  3. {django_components-0.76 → django_components-0.77}/pyproject.toml +1 -1
  4. {django_components-0.76 → django_components-0.77}/src/django_components/component.py +6 -6
  5. {django_components-0.76 → django_components-0.77}/src/django_components/slots.py +46 -39
  6. {django_components-0.76 → django_components-0.77}/src/django_components/templatetags/component_tags.py +25 -18
  7. {django_components-0.76 → django_components-0.77/src/django_components.egg-info}/PKG-INFO +25 -6
  8. {django_components-0.76 → django_components-0.77}/tests/test_templatetags.py +137 -43
  9. {django_components-0.76 → django_components-0.77}/LICENSE +0 -0
  10. {django_components-0.76 → django_components-0.77}/setup.cfg +0 -0
  11. {django_components-0.76 → django_components-0.77}/src/django_components/__init__.py +0 -0
  12. {django_components-0.76 → django_components-0.77}/src/django_components/app_settings.py +0 -0
  13. {django_components-0.76 → django_components-0.77}/src/django_components/apps.py +0 -0
  14. {django_components-0.76 → django_components-0.77}/src/django_components/attributes.py +0 -0
  15. {django_components-0.76 → django_components-0.77}/src/django_components/component_registry.py +0 -0
  16. {django_components-0.76 → django_components-0.77}/src/django_components/context.py +0 -0
  17. {django_components-0.76 → django_components-0.77}/src/django_components/expression.py +0 -0
  18. {django_components-0.76 → django_components-0.77}/src/django_components/logger.py +0 -0
  19. {django_components-0.76 → django_components-0.77}/src/django_components/management/__init__.py +0 -0
  20. {django_components-0.76 → django_components-0.77}/src/django_components/management/commands/__init__.py +0 -0
  21. {django_components-0.76 → django_components-0.77}/src/django_components/management/commands/startcomponent.py +0 -0
  22. {django_components-0.76 → django_components-0.77}/src/django_components/management/commands/upgradecomponent.py +0 -0
  23. {django_components-0.76 → django_components-0.77}/src/django_components/middleware.py +0 -0
  24. {django_components-0.76 → django_components-0.77}/src/django_components/node.py +0 -0
  25. {django_components-0.76 → django_components-0.77}/src/django_components/py.typed +0 -0
  26. {django_components-0.76 → django_components-0.77}/src/django_components/safer_staticfiles/__init__.py +0 -0
  27. {django_components-0.76 → django_components-0.77}/src/django_components/safer_staticfiles/apps.py +0 -0
  28. {django_components-0.76 → django_components-0.77}/src/django_components/template_loader.py +0 -0
  29. {django_components-0.76 → django_components-0.77}/src/django_components/template_parser.py +0 -0
  30. {django_components-0.76 → django_components-0.77}/src/django_components/templatetags/__init__.py +0 -0
  31. {django_components-0.76 → django_components-0.77}/src/django_components/types.py +0 -0
  32. {django_components-0.76 → django_components-0.77}/src/django_components/utils.py +0 -0
  33. {django_components-0.76 → django_components-0.77}/src/django_components.egg-info/SOURCES.txt +0 -0
  34. {django_components-0.76 → django_components-0.77}/src/django_components.egg-info/dependency_links.txt +0 -0
  35. {django_components-0.76 → django_components-0.77}/src/django_components.egg-info/requires.txt +0 -0
  36. {django_components-0.76 → django_components-0.77}/src/django_components.egg-info/top_level.txt +0 -0
  37. {django_components-0.76 → django_components-0.77}/tests/test_attributes.py +0 -0
  38. {django_components-0.76 → django_components-0.77}/tests/test_autodiscover.py +0 -0
  39. {django_components-0.76 → django_components-0.77}/tests/test_component.py +0 -0
  40. {django_components-0.76 → django_components-0.77}/tests/test_component_as_view.py +0 -0
  41. {django_components-0.76 → django_components-0.77}/tests/test_context.py +0 -0
  42. {django_components-0.76 → django_components-0.77}/tests/test_dependency_rendering.py +0 -0
  43. {django_components-0.76 → django_components-0.77}/tests/test_registry.py +0 -0
  44. {django_components-0.76 → django_components-0.77}/tests/test_settings.py +0 -0
  45. {django_components-0.76 → django_components-0.77}/tests/test_startcomponent_command.py +0 -0
  46. {django_components-0.76 → django_components-0.77}/tests/test_template_parser.py +0 -0
  47. {django_components-0.76 → django_components-0.77}/tests/testutils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: django_components
3
- Version: 0.76
3
+ Version: 0.77
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
- 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.
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
- Notice the use of `as "body"` below:
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" as "body" %}{{ body.default }}. Have a great day!{% endfill %}
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
- [slot alias (`as var` syntax)](#accessing-original-content-of-slots)
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" as "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
- 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.
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
- Notice the use of `as "body"` below:
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" as "body" %}{{ body.default }}. Have a great day!{% endfill %}
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
- [slot alias (`as var` syntax)](#accessing-original-content-of-slots)
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" as "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.76"
7
+ version = "0.77"
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"]
@@ -358,8 +358,8 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
358
358
  slot_fills = {
359
359
  slot_name: FillContent(
360
360
  nodes=NodeList([TextNode(escape(content) if escape_content else content)]),
361
- alias=None,
362
- scope=None,
361
+ slot_default_var=None,
362
+ slot_data_var=None,
363
363
  )
364
364
  for (slot_name, content) in slots_data.items()
365
365
  }
@@ -422,12 +422,12 @@ class ComponentNode(Node):
422
422
  f"Detected duplicate fill tag name '{resolved_name}'."
423
423
  )
424
424
 
425
- resolved_fill_alias = fill_node.resolve_alias(context, resolved_component_name)
426
- resolved_scope_var = fill_node.resolve_scope(context, resolved_component_name)
425
+ resolved_slot_default_var = fill_node.resolve_slot_default(context, resolved_component_name)
426
+ resolved_slot_data_var = fill_node.resolve_slot_data(context, resolved_component_name)
427
427
  fill_content[resolved_name] = FillContent(
428
428
  nodes=fill_node.nodelist,
429
- alias=resolved_fill_alias,
430
- scope=resolved_scope_var,
429
+ slot_default_var=resolved_slot_default_var,
430
+ slot_data_var=resolved_slot_data_var,
431
431
  )
432
432
 
433
433
  component: Component = component_cls(
@@ -24,8 +24,8 @@ DEFAULT_SLOT_KEY = "_DJANGO_COMPONENTS_DEFAULT_SLOT"
24
24
 
25
25
  SlotId = str
26
26
  SlotName = str
27
- AliasName = str
28
- ScopeName = str
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
- alias: Optional[AliasName]
48
- scope: Optional[ScopeName]
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
- alias: Optional[AliasName]
84
- scope: Optional[ScopeName]
83
+ slot_default_var: Optional[SlotDefaultName]
84
+ slot_data_var: Optional[SlotDataName]
85
85
 
86
86
 
87
- class UserSlotVar:
87
+ class LazySlot:
88
88
  """
89
- Extensible mechanism for offering 'fill' blocks in template access to properties
90
- of parent slot.
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
- How it works: At render time, SlotNode(s) that have been aliased in the fill tag
93
- of the component instance create an instance of UserSlotVar. This instance is made
94
- available to the rendering context on a key matching the slot alias (see
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
- @property
103
- def default(self) -> str:
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 alias `{% slot "myslot" as "abc" %}`, then set the "abc" to
145
- # the context, so users can refer to the slot from within the slot.
146
- if slot_fill.alias:
147
- if not slot_fill.alias.isidentifier():
148
- raise TemplateSyntaxError(f"Invalid fill alias. Must be a valid identifier. Got '{slot_fill.alias}'")
149
- extra_context[slot_fill.alias] = UserSlotVar(self, context)
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
- if slot_fill.scope:
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[slot_fill.scope] = slot_kwargs
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
- alias_fexp: Optional[FilterExpression] = None,
196
- scope_fexp: Optional[FilterExpression] = None,
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.alias_fexp = alias_fexp
210
+ self.slot_default_var_fexp = slot_default_var_fexp
204
211
  self.is_implicit = is_implicit
205
- self.scope_fexp = scope_fexp
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 resolve_alias(self, context: Context, component_name: Optional[str] = None) -> Optional[str]:
219
- return self.resolve_fexp("alias", self.alias_fexp, context, component_name)
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 resolve_scope(self, context: Context, component_name: Optional[str] = None) -> Optional[str]:
222
- return self.resolve_fexp("scope", self.scope_fexp, context, component_name)
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
- resolved_alias = resolve_expression_as_identifier(context, fexp)
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 resolved_alias
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
- alias=fill.alias,
367
- scope=fill.scope,
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
- alias=None,
456
- scope=None,
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
- alias=default_fill.alias,
504
- scope=default_fill.scope,
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, alias_fexp, scope_var_fexp = _parse_fill_args(parser, args, tag_name)
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
- alias_fexp=alias_fexp,
164
- scope_fexp=scope_var_fexp,
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 " f"{{% fill <name> [{SLOT_DATA_ATTR}=val] [as alias] %}}. "
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
- scope_var_fexp: Optional[FilterExpression] = None
408
+ # Extract known kwargs
409
+ slot_data_var_fexp: Optional[FilterExpression] = None
414
410
  if SLOT_DATA_ATTR in tag_kwargs:
415
- scope_var_fexp = tag_kwargs.pop(SLOT_DATA_ATTR)
416
- if not is_wrapped_in_quotes(scope_var_fexp.token):
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 '{SLOT_DATA_ATTR}' in '{tag_name}' tag must be a string literal, got '{scope_var_fexp}'"
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
- if scope_var_fexp and alias_fexp and scope_var_fexp.token == alias_fexp.token:
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 alias (as ...) and slot data ({SLOT_DATA_ATTR}=...)"
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, alias_fexp, scope_var_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.76
3
+ Version: 0.77
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
- 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.
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
- Notice the use of `as "body"` below:
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" as "body" %}{{ body.default }}. Have a great day!{% endfill %}
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
- [slot alias (`as var` syntax)](#accessing-original-content-of-slots)
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" as "slot_var" %}
883
+ {% fill "content" data="slot_var" default="slot_var" %}
865
884
  {{ slot_var.input }}
866
885
  {% endfill %}
867
886
  {% endcomponent %}
@@ -912,7 +912,7 @@ class ConditionalSlotTests(BaseTestCase):
912
912
  self.assertHTMLEqual(rendered, '<p id="a">Override A</p><p id="b">Override B</p>')
913
913
 
914
914
 
915
- class SlotSuperTests(BaseTestCase):
915
+ class SlotDefaultTests(BaseTestCase):
916
916
  @classmethod
917
917
  def setUpClass(cls):
918
918
  super().setUpClass()
@@ -924,13 +924,13 @@ class SlotSuperTests(BaseTestCase):
924
924
  super().tearDownClass()
925
925
  component.registry.clear()
926
926
 
927
- def test_basic_super_functionality(self):
927
+ def test_basic(self):
928
928
  template_str: types.django_html = """
929
929
  {% load component_tags %}
930
930
  {% component "test" %}
931
- {% fill "header" as "header" %}Before: {{ header.default }}{% endfill %}
932
- {% fill "main" as "main" %}{{ main.default }}{% endfill %}
933
- {% fill "footer" as "footer" %}{{ footer.default }}, after{% endfill %}
931
+ {% fill "header" default="header" %}Before: {{ header }}{% endfill %}
932
+ {% fill "main" default="main" %}{{ main }}{% endfill %}
933
+ {% fill "footer" default="footer" %}{{ footer }}, after{% endfill %}
934
934
  {% endcomponent %}
935
935
  """
936
936
  template = Template(template_str)
@@ -947,13 +947,13 @@ class SlotSuperTests(BaseTestCase):
947
947
  """,
948
948
  )
949
949
 
950
- def test_multiple_super_calls(self):
950
+ def test_multiple_calls(self):
951
951
  template_str: types.django_html = """
952
952
  {% load component_tags %}
953
953
  {% component "test" %}
954
- {% fill "header" as "header" %}
955
- First: {{ header.default }};
956
- Second: {{ header.default }}
954
+ {% fill "header" default="header" %}
955
+ First: {{ header }};
956
+ Second: {{ header }}
957
957
  {% endfill %}
958
958
  {% endcomponent %}
959
959
  """
@@ -971,14 +971,16 @@ class SlotSuperTests(BaseTestCase):
971
971
  """,
972
972
  )
973
973
 
974
- def test_super_under_if_node(self):
974
+ def test_under_if_and_forloop(self):
975
975
  template_str: types.django_html = """
976
976
  {% load component_tags %}
977
977
  {% component "test" %}
978
- {% fill "header" as "header" %}
978
+ {% fill "header" default="header" %}
979
979
  {% for i in range %}
980
- {% if forloop.first %}First {{ header.default }}
981
- {% else %}Later {{ header.default }}
980
+ {% if forloop.first %}
981
+ First {{ header }}
982
+ {% else %}
983
+ Later {{ header }}
982
984
  {% endif %}
983
985
  {% endfor %}
984
986
  {% endfill %}
@@ -998,6 +1000,52 @@ class SlotSuperTests(BaseTestCase):
998
1000
  """,
999
1001
  )
1000
1002
 
1003
+ def test_nested_fills(self):
1004
+ template_str: types.django_html = """
1005
+ {% load component_tags %}
1006
+ {% component "test" %}
1007
+ {% fill "header" default="header1" %}
1008
+ header1_in_header1: {{ header1 }}
1009
+ {% component "test" %}
1010
+ {% fill "header" default="header2" %}
1011
+ header1_in_header2: {{ header1 }}
1012
+ header2_in_header2: {{ header2 }}
1013
+ {% endfill %}
1014
+ {% fill "footer" default="footer2" %}
1015
+ header1_in_footer2: {{ header1 }}
1016
+ footer2_in_footer2: {{ footer2 }}
1017
+ {% endfill %}
1018
+ {% endcomponent %}
1019
+ {% endfill %}
1020
+ {% endcomponent %}
1021
+ """
1022
+ template = Template(template_str)
1023
+ rendered = template.render(Context({}))
1024
+
1025
+ self.assertHTMLEqual(
1026
+ rendered,
1027
+ """
1028
+ <custom-template>
1029
+ <header>
1030
+ header1_in_header1: Default header
1031
+ <custom-template>
1032
+ <header>
1033
+ header1_in_header2: Default header
1034
+ header2_in_header2: Default header
1035
+ </header>
1036
+ <main>Default main</main>
1037
+ <footer>
1038
+ header1_in_footer2: Default header
1039
+ footer2_in_footer2: Default footer
1040
+ </footer>
1041
+ </custom-template>
1042
+ </header>
1043
+ <main>Default main</main>
1044
+ <footer>Default footer</footer>
1045
+ </custom-template>
1046
+ """,
1047
+ )
1048
+
1001
1049
 
1002
1050
  class TemplateSyntaxErrorTests(BaseTestCase):
1003
1051
  @classmethod
@@ -1310,11 +1358,11 @@ class ComponentNestingTests(BaseTestCase):
1310
1358
  self.assertHTMLEqual(rendered, expected)
1311
1359
 
1312
1360
  @override_settings(COMPONENTS={"context_behavior": "django"})
1313
- def test_component_nesting_component_with_fill_and_super__django(self):
1361
+ def test_component_nesting_component_with_slot_default__django(self):
1314
1362
  template_str: types.django_html = """
1315
1363
  {% load component_tags %}
1316
1364
  {% component "dashboard" %}
1317
- {% fill "header" as "h" %} Hello! {{ h.default }} {% endfill %}
1365
+ {% fill "header" default="h" %} Hello! {{ h }} {% endfill %}
1318
1366
  {% endcomponent %}
1319
1367
  """
1320
1368
  template = Template(template_str)
@@ -1338,11 +1386,11 @@ class ComponentNestingTests(BaseTestCase):
1338
1386
  self.assertHTMLEqual(rendered, expected)
1339
1387
 
1340
1388
  @override_settings(COMPONENTS={"context_behavior": "isolated"})
1341
- def test_component_nesting_component_with_fill_and_super__isolated(self):
1389
+ def test_component_nesting_component_with_slot_default__isolated(self):
1342
1390
  template_str: types.django_html = """
1343
1391
  {% load component_tags %}
1344
1392
  {% component "dashboard" %}
1345
- {% fill "header" as "h" %} Hello! {{ h.default }} {% endfill %}
1393
+ {% fill "header" default="h" %} Hello! {{ h }} {% endfill %}
1346
1394
  {% endcomponent %}
1347
1395
  """
1348
1396
  template = Template(template_str)
@@ -2194,8 +2242,8 @@ class IterationFillTest(BaseTestCase):
2194
2242
  {% component "slot_in_a_loop" objects=objects %}
2195
2243
  {% fill "slot_inner" %}
2196
2244
  {% component "slot_in_a_loop" objects=object.inner %}
2197
- {% fill "slot_inner" as "super_slot_inner" %}
2198
- {{ super_slot_inner.default }}
2245
+ {% fill "slot_inner" default="super_slot_inner" %}
2246
+ {{ super_slot_inner }}
2199
2247
  {% endfill %}
2200
2248
  {% endcomponent %}
2201
2249
  {% endfill %}
@@ -2228,8 +2276,8 @@ class IterationFillTest(BaseTestCase):
2228
2276
  {% component "slot_in_a_loop" objects=objects %}
2229
2277
  {% fill "slot_inner" %}
2230
2278
  {% component "slot_in_a_loop" objects=object.inner %}
2231
- {% fill "slot_inner" as "super_slot_inner" %}
2232
- {{ super_slot_inner.default }}
2279
+ {% fill "slot_inner" default="super_slot_inner" %}
2280
+ {{ super_slot_inner }}
2233
2281
  {% endfill %}
2234
2282
  {% endcomponent %}
2235
2283
  {% endfill %}
@@ -2256,9 +2304,9 @@ class IterationFillTest(BaseTestCase):
2256
2304
  {% fill "slot_inner" %}
2257
2305
  {{ outer_scope_variable_1 }}
2258
2306
  {% component "slot_in_a_loop" objects=object.inner %}
2259
- {% fill "slot_inner" as "super_slot_inner" %}
2307
+ {% fill "slot_inner" default="super_slot_inner" %}
2260
2308
  {{ outer_scope_variable_2 }}
2261
- {{ super_slot_inner.default }}
2309
+ {{ super_slot_inner }}
2262
2310
  {% endfill %}
2263
2311
  {% endcomponent %}
2264
2312
  {% endfill %}
@@ -2311,9 +2359,9 @@ class IterationFillTest(BaseTestCase):
2311
2359
  {% fill "slot_inner" %}
2312
2360
  {{ outer_scope_variable_1 }}
2313
2361
  {% component "slot_in_a_loop" objects=object.inner %}
2314
- {% fill "slot_inner" as "super_slot_inner" %}
2362
+ {% fill "slot_inner" default="super_slot_inner" %}
2315
2363
  {{ outer_scope_variable_2 }}
2316
- {{ super_slot_inner.default }}
2364
+ {{ super_slot_inner }}
2317
2365
  {% endfill %}
2318
2366
  {% endcomponent %}
2319
2367
  {% endfill %}
@@ -2358,9 +2406,9 @@ class IterationFillTest(BaseTestCase):
2358
2406
  {% fill "slot_inner" %}
2359
2407
  {{ outer_scope_variable_1|safe }}
2360
2408
  {% component "slot_in_a_loop" objects=objects %}
2361
- {% fill "slot_inner" as "super_slot_inner" %}
2409
+ {% fill "slot_inner" default="super_slot_inner" %}
2362
2410
  {{ outer_scope_variable_2|safe }}
2363
- {{ super_slot_inner.default }}
2411
+ {{ super_slot_inner }}
2364
2412
  {% endfill %}
2365
2413
  {% endcomponent %}
2366
2414
  {% endfill %}
@@ -2401,7 +2449,7 @@ class ScopedSlotTest(BaseTestCase):
2401
2449
  template: types.django_html = """
2402
2450
  {% load component_tags %}
2403
2451
  <div>
2404
- {% slot "my_slot" abc=abc def=var123 %}Default text{% endslot %}
2452
+ {% slot "my_slot" abc=abc var123=var123 %}Default text{% endslot %}
2405
2453
  </div>
2406
2454
  """
2407
2455
 
@@ -2416,7 +2464,7 @@ class ScopedSlotTest(BaseTestCase):
2416
2464
  {% component "test" %}
2417
2465
  {% fill "my_slot" data="slot_data_in_fill" %}
2418
2466
  {{ slot_data_in_fill.abc }}
2419
- {{ slot_data_in_fill.def }}
2467
+ {{ slot_data_in_fill.var123 }}
2420
2468
  {% endfill %}
2421
2469
  {% endcomponent %}
2422
2470
  """
@@ -2435,7 +2483,7 @@ class ScopedSlotTest(BaseTestCase):
2435
2483
  template: types.django_html = """
2436
2484
  {% load component_tags %}
2437
2485
  <div>
2438
- {% slot "my_slot" default abc=abc 123=var123 required %}Default text{% endslot %}
2486
+ {% slot "my_slot" default abc=abc var123=var123 required %}Default text{% endslot %}
2439
2487
  </div>
2440
2488
  """
2441
2489
 
@@ -2450,7 +2498,7 @@ class ScopedSlotTest(BaseTestCase):
2450
2498
  {% component "test" %}
2451
2499
  {% fill "my_slot" data="slot_data_in_fill" %}
2452
2500
  {{ slot_data_in_fill.abc }}
2453
- {{ slot_data_in_fill.123 }}
2501
+ {{ slot_data_in_fill.var123 }}
2454
2502
  {% endfill %}
2455
2503
  {% endcomponent %}
2456
2504
  """
@@ -2463,13 +2511,13 @@ class ScopedSlotTest(BaseTestCase):
2463
2511
  """
2464
2512
  self.assertHTMLEqual(rendered, expected)
2465
2513
 
2466
- def test_slot_data_fill_with_as(self):
2514
+ def test_slot_data_with_slot_default(self):
2467
2515
  @component.register("test")
2468
2516
  class TestComponent(component.Component):
2469
2517
  template: types.django_html = """
2470
2518
  {% load component_tags %}
2471
2519
  <div>
2472
- {% slot "my_slot" abc=abc 123=var123 %}Default text{% endslot %}
2520
+ {% slot "my_slot" abc=abc var123=var123 %}Default text{% endslot %}
2473
2521
  </div>
2474
2522
  """
2475
2523
 
@@ -2482,10 +2530,10 @@ class ScopedSlotTest(BaseTestCase):
2482
2530
  template: types.django_html = """
2483
2531
  {% load component_tags %}
2484
2532
  {% component "test" %}
2485
- {% fill "my_slot" data="slot_data_in_fill" as "slot_var" %}
2486
- {{ slot_var.default }}
2533
+ {% fill "my_slot" data="slot_data_in_fill" default="slot_var" %}
2534
+ {{ slot_var }}
2487
2535
  {{ slot_data_in_fill.abc }}
2488
- {{ slot_data_in_fill.123 }}
2536
+ {{ slot_data_in_fill.var123 }}
2489
2537
  {% endfill %}
2490
2538
  {% endcomponent %}
2491
2539
  """
@@ -2499,13 +2547,13 @@ class ScopedSlotTest(BaseTestCase):
2499
2547
  """
2500
2548
  self.assertHTMLEqual(rendered, expected)
2501
2549
 
2502
- def test_slot_data_raises_on_slot_data_and_as_same_var(self):
2550
+ def test_slot_data_raises_on_slot_data_and_slot_default_same_var(self):
2503
2551
  @component.register("test")
2504
2552
  class TestComponent(component.Component):
2505
2553
  template: types.django_html = """
2506
2554
  {% load component_tags %}
2507
2555
  <div>
2508
- {% slot "my_slot" abc=abc 123=var123 %}Default text{% endslot %}
2556
+ {% slot "my_slot" abc=abc var123=var123 %}Default text{% endslot %}
2509
2557
  </div>
2510
2558
  """
2511
2559
 
@@ -2518,14 +2566,14 @@ class ScopedSlotTest(BaseTestCase):
2518
2566
  template: types.django_html = """
2519
2567
  {% load component_tags %}
2520
2568
  {% component "test" %}
2521
- {% fill "my_slot" data="slot_var" as "slot_var" %}
2522
- {{ slot_var.default }}
2569
+ {% fill "my_slot" data="slot_var" default="slot_var" %}
2570
+ {{ slot_var }}
2523
2571
  {% endfill %}
2524
2572
  {% endcomponent %}
2525
2573
  """
2526
2574
  with self.assertRaisesMessage(
2527
2575
  TemplateSyntaxError,
2528
- "'fill' received the same string for slot alias (as ...) and slot data (data=...)",
2576
+ "'fill' received the same string for slot default (default=...) and slot data (data=...)",
2529
2577
  ):
2530
2578
  Template(template).render(Context())
2531
2579
 
@@ -2535,7 +2583,7 @@ class ScopedSlotTest(BaseTestCase):
2535
2583
  template: types.django_html = """
2536
2584
  {% load component_tags %}
2537
2585
  <div>
2538
- {% slot "my_slot" abc=abc 123=var123 %}Default text{% endslot %}
2586
+ {% slot "my_slot" abc=abc var123=var123 %}Default text{% endslot %}
2539
2587
  </div>
2540
2588
  """
2541
2589
 
@@ -2585,7 +2633,7 @@ class ScopedSlotTest(BaseTestCase):
2585
2633
  template: types.django_html = """
2586
2634
  {% load component_tags %}
2587
2635
  <div>
2588
- {% slot "my_slot" abc=abc 123=var123 %}Default text{% endslot %}
2636
+ {% slot "my_slot" abc=abc var123=var123 %}Default text{% endslot %}
2589
2637
  </div>
2590
2638
  """
2591
2639
 
@@ -2603,3 +2651,49 @@ class ScopedSlotTest(BaseTestCase):
2603
2651
  rendered = Template(template).render(Context())
2604
2652
  expected = "<div> Default text </div>"
2605
2653
  self.assertHTMLEqual(rendered, expected)
2654
+
2655
+ def test_nested_fills(self):
2656
+ @component.register("test")
2657
+ class TestComponent(component.Component):
2658
+ template: types.django_html = """
2659
+ {% load component_tags %}
2660
+ <div>
2661
+ {% slot "my_slot" abc=abc input=input %}Default text{% endslot %}
2662
+ </div>
2663
+ """
2664
+
2665
+ def get_context_data(self, input):
2666
+ return {
2667
+ "abc": "def",
2668
+ "input": input,
2669
+ }
2670
+
2671
+ template_str: types.django_html = """
2672
+ {% load component_tags %}
2673
+ {% component "test" input=1 %}
2674
+ {% fill "my_slot" data="data1" %}
2675
+ data1_in_slot1: {{ data1|safe }}
2676
+ {% component "test" input=2 %}
2677
+ {% fill "my_slot" data="data2" %}
2678
+ data1_in_slot2: {{ data1|safe }}
2679
+ data2_in_slot2: {{ data2|safe }}
2680
+ {% endfill %}
2681
+ {% endcomponent %}
2682
+ {% endfill %}
2683
+ {% endcomponent %}
2684
+ """
2685
+ template = Template(template_str)
2686
+ rendered = template.render(Context({}))
2687
+
2688
+ self.assertHTMLEqual(
2689
+ rendered,
2690
+ """
2691
+ <div>
2692
+ data1_in_slot1: {'abc': 'def', 'input': 1}
2693
+ <div>
2694
+ data1_in_slot2: {'abc': 'def', 'input': 1}
2695
+ data2_in_slot2: {'abc': 'def', 'input': 2}
2696
+ </div>
2697
+ </div>
2698
+ """,
2699
+ )