mirror of
https://github.com/django-components/django-components.git
synced 2025-08-10 01:08:00 +00:00
feat: capture root context
This commit is contained in:
parent
8d3a2ba8db
commit
ec12a3bcb8
2 changed files with 15 additions and 0 deletions
|
@ -28,6 +28,7 @@ from django_components.slots import (
|
||||||
FillContent,
|
FillContent,
|
||||||
SlotName,
|
SlotName,
|
||||||
render_component_template_with_slots,
|
render_component_template_with_slots,
|
||||||
|
OUTER_CONTEXT_CONTEXT_KEY,
|
||||||
DEFAULT_SLOT_KEY,
|
DEFAULT_SLOT_KEY,
|
||||||
)
|
)
|
||||||
from django_components.utils import search
|
from django_components.utils import search
|
||||||
|
@ -317,6 +318,12 @@ class ComponentNode(Node):
|
||||||
resolved_component_name = self.name_fexp.resolve(context)
|
resolved_component_name = self.name_fexp.resolve(context)
|
||||||
component_cls: Type[Component] = registry.get(resolved_component_name)
|
component_cls: Type[Component] = registry.get(resolved_component_name)
|
||||||
|
|
||||||
|
# If this is the outer-/top-most component node, then save the outer context,
|
||||||
|
# so it can be used by nested Slots.
|
||||||
|
root_ctx_already_defined = OUTER_CONTEXT_CONTEXT_KEY in context
|
||||||
|
if not root_ctx_already_defined:
|
||||||
|
context.push({ OUTER_CONTEXT_CONTEXT_KEY: context.__copy__() })
|
||||||
|
|
||||||
# Resolve FilterExpressions and Variables that were passed as args to the
|
# Resolve FilterExpressions and Variables that were passed as args to the
|
||||||
# component, then call component's context method
|
# component, then call component's context method
|
||||||
# to get values to insert into the context
|
# to get values to insert into the context
|
||||||
|
@ -344,8 +351,15 @@ class ComponentNode(Node):
|
||||||
|
|
||||||
component_context: dict = component.get_context_data(*resolved_context_args, **resolved_context_kwargs)
|
component_context: dict = component.get_context_data(*resolved_context_args, **resolved_context_kwargs)
|
||||||
|
|
||||||
|
# Prevent outer context from leaking into the template of the component
|
||||||
if self.isolated_context:
|
if self.isolated_context:
|
||||||
|
# Even if contexts are isolated, we still need to pass down the
|
||||||
|
# original context so variables in slots can be rendered using
|
||||||
|
# the original context.
|
||||||
|
orig_ctx = context
|
||||||
context = context.new()
|
context = context.new()
|
||||||
|
context.push({ OUTER_CONTEXT_CONTEXT_KEY: orig_ctx })
|
||||||
|
|
||||||
with context.update(component_context):
|
with context.update(component_context):
|
||||||
rendered_component = component.render(context)
|
rendered_component = component.render(context)
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ from django.utils.safestring import SafeString, mark_safe
|
||||||
|
|
||||||
FILLED_SLOTS_CONTENT_CONTEXT_KEY = "_DJANGO_COMPONENTS_FILLED_SLOTS"
|
FILLED_SLOTS_CONTENT_CONTEXT_KEY = "_DJANGO_COMPONENTS_FILLED_SLOTS"
|
||||||
DEFAULT_SLOT_KEY = "_DJANGO_COMPONENTS_DEFAULT_SLOT"
|
DEFAULT_SLOT_KEY = "_DJANGO_COMPONENTS_DEFAULT_SLOT"
|
||||||
|
OUTER_CONTEXT_CONTEXT_KEY = "_DJANGO_COMPONENTS_OUTER_CONTEXT"
|
||||||
|
|
||||||
# Type aliases
|
# Type aliases
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue