Skip to content

Commit

Permalink
Merge pull request #144 from wrabit/fix_valueless_cvars
Browse files Browse the repository at this point in the history
fixed compiler setting empty cvars to None
  • Loading branch information
wrabit authored Sep 15, 2024
2 parents ba5b9c5 + 5da7140 commit d256a27
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
13 changes: 9 additions & 4 deletions django_cotton/cotton_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <c-vars>
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 %}"

Expand Down
7 changes: 5 additions & 2 deletions django_cotton/templatetags/_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}):
Expand Down
30 changes: 29 additions & 1 deletion django_cotton/tests/test_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
"""
<c-empty-cvars />
""",
"view/",
)

self.create_template(
"cotton/empty_cvars.html",
""",
<c-vars test1="None" test2="" test3 test4=None />
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'")
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
document.documentElement.classList.remove('dark')
}
</script>
<style>[x-cloak] { display: none }</style>
<style>
/* alpine */
[x-cloak] { display: none }
/* lastpass */
div[data-lastpass-icon-root] { display: none }
</style>
</head>
<body class="bg-amber-50 dark:bg-gray-900 flex flex-col min-h-screen bg-fixed text-[16.5px]">
<c-navbar />
Expand Down

0 comments on commit d256a27

Please sign in to comment.