Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #51: Pass component context to children. #52

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions slippers/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,14 @@ def SLIPPERS_TYPE_CHECKING_OUTPUT(
["console", "overlay"],
)

@property
def SLIPPERS_PARAMS_VISIBLE_IN_CHILDREN(self) -> bool:
"""Make component parameters visible in children"""
return getattr(django_settings, "SLIPPERS_PARAMS_VISIBLE_IN_CHILDREN", False) # type: ignore

@property
def SLIPPERS_VARS_VISIBLE_IN_CHILDREN(self) -> bool:
"""Make component var declarations visible in children"""
return getattr(django_settings, "SLIPPERS_VARS_VISIBLE_IN_CHILDREN", False) # type: ignore

settings = Settings()
33 changes: 31 additions & 2 deletions slippers/templatetags/slippers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django import template
from django.conf import settings as django_settings
from django.template import Context, NodeList
from django.template.base import VariableNode
from django.utils.safestring import mark_safe

from slippers.conf import settings
Expand Down Expand Up @@ -93,14 +94,42 @@ def __init__(
self.target_var = target_var

def render(self, context):
children = self.nodelist.render(context) if self.nodelist else ""

attributes = {
key: value.resolve(context) for key, value in self.raw_attributes.items()
}

template = context.template.engine.get_template(self.template_path)

if not self.nodelist:
children = ""
else:
# Append the attributes to the context's dict stack. This avoids
# copying it. Make sure to pop it off afterwards.
# template.nodelist contains var declarations which we can use, too.
want_params = settings.SLIPPERS_PARAMS_VISIBLE_IN_CHILDREN
want_vars = settings.SLIPPERS_VARS_VISIBLE_IN_CHILDREN

# if 'test_component' in self.template_path: import pdb; pdb.set_trace()
if want_params:
context.dicts.append(attributes)
if want_vars:
variables = {}
context.dicts.append(variables)
for node in template.nodelist:
if isinstance(node, VarNode):
variables.update({name: value.resolve(context) for name, value in node.var_map.items()})
elif isinstance(node, VariableNode):
fe = node.filter_expression
if fe.is_var and fe.var.var == 'children':
break
try:
children = self.nodelist.render(context)
finally:
if want_vars:
context.dicts.pop()
if want_params:
context.dicts.pop()

source_front_matter = extract_template_parts(template.source)[0]

prop_errors = None
Expand Down