diff --git a/django_cotton/cotton_loader.py b/django_cotton/cotton_loader.py index 5edbefa..90bc416 100755 --- a/django_cotton/cotton_loader.py +++ b/django_cotton/cotton_loader.py @@ -227,12 +227,17 @@ def _wrap_with_cotton_vars_frame(self, soup, cvars_el): same component's context, we wrap the entire contents in another component: cotton_vars_frame. Only when is present.""" - cvars_attrs_string = " ".join( - f'{k}="{" ".join(v) if k == "class" else v}"' for k, v in cvars_el.attrs.items() - ) + cvars_attrs = [] + for k, v in cvars_el.attrs.items(): + if v is None: + cvars_attrs.append(k) + else: + if k == "class": + v = " ".join(v) + cvars_attrs.append(f'{k}="{v}"') cvars_el.decompose() - opening = f"{{% vars {cvars_attrs_string} %}}" + opening = f"{{% vars {' '.join(cvars_attrs)} %}}" opening = opening.replace("\n", "") closing = "{% endvars %}" diff --git a/django_cotton/templatetags/_vars.py b/django_cotton/templatetags/_vars.py index dc6af4a..ab622e5 100644 --- a/django_cotton/templatetags/_vars.py +++ b/django_cotton/templatetags/_vars.py @@ -24,9 +24,9 @@ def render(self, context): current_component = cotton_data["stack"][-1] attrs = current_component["attrs"] - # Process and resolve the merged vars + print(self.var_dict.items()) + for key, value in self.var_dict.items(): - attrs.exclude_from_string_output(key) if key not in attrs.exclude_unprocessable(): if key.startswith(":"): try: @@ -39,9 +39,12 @@ def render(self, context): except (VariableDoesNotExist, IndexError): resolved_value = value attrs[key] = resolved_value + attrs.exclude_from_string_output(key) # Process cvars without values for empty_var in self.empty_vars: + # if empty_var in attrs.exclude_unprocessable(): + attrs.exclude_from_string_output(empty_var) with context.push({**attrs.make_attrs_accessible(), "attrs": attrs}): diff --git a/django_cotton/tests/test_slots.py b/django_cotton/tests/test_slots.py index c23ca8d..b6bd2c5 100644 --- a/django_cotton/tests/test_slots.py +++ b/django_cotton/tests/test_slots.py @@ -2,7 +2,7 @@ from django_cotton.tests.utils import get_compiled -class SlotAndContentTests(CottonTestCase): +class SlotTests(CottonTestCase): def test_named_slots_correctly_display_in_loop(self): self.create_template( "named_slot_in_loop_view.html", @@ -75,3 +75,31 @@ def test_vars_are_converted_to_vars_frame_tags(self): compiled, """{% vars var1="string with space" %}content{% endvars %}""", ) + + def test_named_slot_missing(self): + self.create_template( + "test_empty_cvars_view.html", + """ + + """, + "view/", + ) + + self.create_template( + "cotton/empty_cvars.html", + """, + + + test1: '{{ test1 }}' + test2: '{{ test2 }}' + test3: '{{ test3 }}' + test4: '{{ test4 }}' + """, + ) + + with self.settings(ROOT_URLCONF=self.url_conf()): + response = self.client.get("/view/") + self.assertContains(response, "test1: 'None'") + self.assertContains(response, "test2: ''") + self.assertContains(response, "test3: ''") + self.assertContains(response, "test4: 'None'") diff --git a/docs/docs_project/docs_project/templates/cotton/layouts/base.html b/docs/docs_project/docs_project/templates/cotton/layouts/base.html index 86c775f..8791399 100644 --- a/docs/docs_project/docs_project/templates/cotton/layouts/base.html +++ b/docs/docs_project/docs_project/templates/cotton/layouts/base.html @@ -25,7 +25,12 @@ document.documentElement.classList.remove('dark') } - +