Merge pull request #30 from darth-veitcher/feat/jinja2-fromjson-filter

feat(format-string): add fromjson Jinja2 filter
This commit is contained in:
James Veitch
2026-07-24 22:40:32 +01:00
committed by GitHub
2 changed files with 16 additions and 0 deletions
+4
View File
@@ -75,6 +75,10 @@ class FormatString:
# Create a sandboxed Jinja2 environment for security
jinja_env = sandbox.SandboxedEnvironment()
# Jinja2 ships `tojson` but not its inverse; add one so STRING inputs
# carrying a JSON array/object (ComfyUI has no native list socket type)
# can be parsed back into real Python data, e.g. `{{ hints | fromjson }}`.
jinja_env.filters["fromjson"] = json.loads
# Define additional context
@staticmethod
+12
View File
@@ -201,6 +201,18 @@ class TestJinja2Formatting:
assert result[2] == "John"
assert result[3] == "Doe"
def test_jinja2_fromjson_filter_parses_array(self, format_string_class):
"""ComfyUI has no native list socket, so a STRING input carrying a
JSON array must be parseable back into a real list for iteration."""
result = format_string_class.format_string(
template_type="Jinja2",
template="{% for hint in hints | fromjson %}- {{ hint }}\n{% endfor %}",
save_path="",
unique_id="test-fromjson",
hints='["motion", "camera pan"]',
)["result"]
assert result[0] == "- motion\n- camera pan\n"
def test_jinja2_with_datetime(self, format_string_class):
"""Test Jinja2 formatting with datetime context."""
result = format_string_class.format_string(