diff --git a/django_cotton/templatetags/_vars.py b/django_cotton/templatetags/_vars.py
index cc452fa..3984975 100644
--- a/django_cotton/templatetags/_vars.py
+++ b/django_cotton/templatetags/_vars.py
@@ -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
@@ -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(" tag must be used inside a component")
+ return output
def cotton_cvars(parser, token):
diff --git a/django_cotton/tests/test_cvars.py b/django_cotton/tests/test_cvars.py
index 86cbfe7..ba951ae 100644
--- a/django_cotton/tests/test_cvars.py
+++ b/django_cotton/tests/test_cvars.py
@@ -1,3 +1,5 @@
+from django.template.loader import render_to_string
+
from django_cotton.tests.utils import CottonTestCase
@@ -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",
+ """
+
+
+ {{ cvar|safe }}
+ """,
+ )
+
+ rendered = render_to_string("cotton/direct_render.html")
+
+ self.assertTrue("I'm all set" in rendered)