mirror of
https://github.com/wrabit/django-cotton.git
synced 2025-08-04 07:08:21 +00:00
fixed case when cvars from modifying global context
This commit is contained in:
parent
df068b787c
commit
45a89d774b
2 changed files with 43 additions and 24 deletions
|
@ -43,15 +43,15 @@ class CottonVarsFrameNode(template.Node):
|
|||
c_vars[key] = value.resolve(context)
|
||||
|
||||
# Excluding keys from {{ attrs }} that are identified as vars
|
||||
attrs_dict = {k: v for k, v in provided_attrs.items() if k not in c_vars}
|
||||
remaining_attrs_dict = {k: v for k, v in provided_attrs.items() if k not in c_vars}
|
||||
attrs_string = " ".join(f"{k}={ensure_quoted(v)}" for k, v in remaining_attrs_dict.items())
|
||||
accessible_names = {key.replace("-", "_"): value for key, value in c_vars.items()}
|
||||
|
||||
# Provide all of the attrs as a string to pass to the component before any '-' to '_' replacing
|
||||
attrs_string = " ".join(f"{k}={ensure_quoted(v)}" for k, v in attrs_dict.items())
|
||||
context["attrs"] = mark_safe(attrs_string)
|
||||
context["attrs_dict"] = attrs_dict
|
||||
tmp_context = {
|
||||
"attrs_dict": remaining_attrs_dict,
|
||||
"attrs": mark_safe(attrs_string),
|
||||
**accessible_names,
|
||||
}
|
||||
|
||||
# Store attr names in a callable format, i.e. 'x-init' will be accessible by {{ x_init }} when called explicitly and not in {{ attrs }}
|
||||
formatted_vars = {key.replace("-", "_"): value for key, value in c_vars.items()}
|
||||
context.update(formatted_vars)
|
||||
|
||||
return self.nodelist.render(context)
|
||||
with context.push(**tmp_context):
|
||||
return self.nodelist.render(context)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from django_cotton.tests.utils import CottonTestCase, get_rendered
|
||||
from django_cotton.tests.utils import CottonTestCase
|
||||
from django_cotton.tests.utils import get_compiled
|
||||
|
||||
|
||||
|
@ -135,17 +135,36 @@ class TemplateRenderingTests(CottonTestCase):
|
|||
response = self.client.get("/view/", data={"foo": "bar"})
|
||||
self.assertContains(response, "?foo=bar")
|
||||
|
||||
def test_cvars_rendering(self):
|
||||
html = """
|
||||
<c-vars stroke_width="30" />
|
||||
|
||||
<svg {{ attrs }} viewBox="0 0 512 512">
|
||||
<g fill="none" stroke="currentColor" stroke-width="{{ stroke_width }}" stroke-linejoin="round">
|
||||
<path d="M143.533 256 79.267 384.533v-192.8L497 127.467z"/>
|
||||
<path d="M143.533 256 79.267 384.533l119.352-73.448zM15 127.467h482L79.267 191.733z"/>
|
||||
<path d="M143.533 256 497 127.467l-241 241z"/>
|
||||
</g>
|
||||
</svg>
|
||||
"""
|
||||
def test_cvars_isnt_changing_global_context(self):
|
||||
self.create_template(
|
||||
"cotton/child.html",
|
||||
"""
|
||||
<c-vars />
|
||||
|
||||
name: child (class: {{ class }})
|
||||
""",
|
||||
)
|
||||
self.create_template(
|
||||
"cotton/parent.html",
|
||||
"""
|
||||
name: parent (class: {{ class }}))
|
||||
|
||||
{{ slot }}
|
||||
""",
|
||||
)
|
||||
|
||||
print(get_rendered(html))
|
||||
self.create_template(
|
||||
"slot_scope_view.html",
|
||||
"""
|
||||
<c-parent>
|
||||
<c-child class="testy" />
|
||||
</c-parent>
|
||||
""",
|
||||
"view/",
|
||||
)
|
||||
|
||||
# Override URLconf
|
||||
with self.settings(ROOT_URLCONF=self.url_conf()):
|
||||
response = self.client.get("/view/")
|
||||
self.assertTrue("name: child (class: testy)" in response.content.decode())
|
||||
self.assertTrue("name: parent (class: )" in response.content.decode())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue