mirror of
https://github.com/django-components/django-components.git
synced 2025-08-08 08:17:59 +00:00
refactor: fix slot context behavior (#445)
This commit is contained in:
parent
f3d6337ecc
commit
3ad0dd8677
6 changed files with 287 additions and 80 deletions
|
@ -20,12 +20,7 @@ from django.views import View
|
|||
# way the two modules depend on one another.
|
||||
from django_components.component_registry import registry # NOQA
|
||||
from django_components.component_registry import AlreadyRegistered, ComponentRegistry, NotRegistered, register # NOQA
|
||||
from django_components.context import (
|
||||
capture_root_context,
|
||||
get_root_context,
|
||||
set_root_context,
|
||||
set_slot_component_association,
|
||||
)
|
||||
from django_components.context import make_isolated_context_copy, prepare_context, set_slot_component_association
|
||||
from django_components.logger import logger, trace_msg
|
||||
from django_components.middleware import is_dependency_middleware_active
|
||||
from django_components.node import walk_nodelist
|
||||
|
@ -268,6 +263,7 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|||
# NOTE: This if/else is important to avoid nested Contexts,
|
||||
# See https://github.com/EmilStenstrom/django-components/issues/414
|
||||
context = context_data if isinstance(context_data, Context) else Context(context_data)
|
||||
prepare_context(context, outer_context=self.outer_context or Context())
|
||||
template = self.get_template(context)
|
||||
|
||||
# Associate the slots with this component for this context
|
||||
|
@ -347,10 +343,6 @@ class ComponentNode(Node):
|
|||
resolved_component_name = self.name_fexp.resolve(context)
|
||||
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.
|
||||
capture_root_context(context)
|
||||
|
||||
# Resolve FilterExpressions and Variables that were passed as args to the
|
||||
# component, then call component's context method
|
||||
# to get values to insert into the context
|
||||
|
@ -380,12 +372,7 @@ class ComponentNode(Node):
|
|||
|
||||
# Prevent outer context from leaking into the template of the component
|
||||
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.
|
||||
root_ctx = get_root_context(context)
|
||||
context = context.new()
|
||||
set_root_context(context, root_ctx)
|
||||
context = make_isolated_context_copy(context)
|
||||
|
||||
with context.update(component_context):
|
||||
rendered_component = component.render(context)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue