Skip to content

Commit

Permalink
Merge pull request #202 from wrabit/201_render_to_string_for_comps
Browse files Browse the repository at this point in the history
Fixes direct rendering components that use cvars
  • Loading branch information
wrabit authored Oct 16, 2024
2 parents d2ec298 + 58408f2 commit e018c19
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
49 changes: 24 additions & 25 deletions django_cotton/templatetags/_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
Variable,
VariableDoesNotExist,
Node,
TemplateSyntaxError,
)

from django_cotton.templatetags import DynamicAttr, UnprocessableDynamicAttr
from django_cotton.templatetags import DynamicAttr, UnprocessableDynamicAttr, Attrs
from django_cotton.utils import get_cotton_data


Expand All @@ -23,34 +22,34 @@ def render(self, context):
if cotton_data["stack"]:
current_component = cotton_data["stack"][-1]
attrs = current_component["attrs"]
vars = {}
else:
attrs = Attrs({})

for key, value in self.var_dict.items():
if key not in attrs.exclude_unprocessable():
if key.startswith(":"):
try:
vars[key[1:]] = DynamicAttr(value, is_cvar=True).resolve(context)
except UnprocessableDynamicAttr:
pass
else:
try:
resolved_value = Variable(value).resolve(context)
except (VariableDoesNotExist, IndexError):
resolved_value = value
attrs[key] = resolved_value
attrs.exclude_from_string_output(key)
vars = {}

# Process cvars without values
for empty_var in self.empty_vars:
attrs.exclude_from_string_output(empty_var)
for key, value in self.var_dict.items():
if key not in attrs.exclude_unprocessable():
if key.startswith(":"):
try:
vars[key[1:]] = DynamicAttr(value, is_cvar=True).resolve(context)
except UnprocessableDynamicAttr:
pass
else:
try:
resolved_value = Variable(value).resolve(context)
except (VariableDoesNotExist, IndexError):
resolved_value = value
attrs[key] = resolved_value
attrs.exclude_from_string_output(key)

with context.push({**vars, **attrs.make_attrs_accessible(), "attrs": attrs}):
output = self.nodelist.render(context)
# Process cvars without values
for empty_var in self.empty_vars:
attrs.exclude_from_string_output(empty_var)

return output
with context.push({**vars, **attrs.make_attrs_accessible(), "attrs": attrs}):
output = self.nodelist.render(context)

else:
raise TemplateSyntaxError("<c-vars /> tag must be used inside a component")
return output


def cotton_cvars(parser, token):
Expand Down
16 changes: 16 additions & 0 deletions django_cotton/tests/test_cvars.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.template.loader import render_to_string

from django_cotton.tests.utils import CottonTestCase


Expand Down Expand Up @@ -364,3 +366,17 @@ def test_incoming_dynamic_attributes_overwrite_cvars_dynamic_attributes(self):
response = self.client.get("/view/")
self.assertContains(response, "expected")
self.assertNotContains(response, "not")

def test_cvars_are_processed_when_component_rendered_using_render_to_string(self):
self.create_template(
"cotton/direct_render.html",
"""
<c-vars cvar="I'm all set" />
{{ cvar|safe }}
""",
)

rendered = render_to_string("cotton/direct_render.html")

self.assertTrue("I'm all set" in rendered)

0 comments on commit e018c19

Please sign in to comment.