Skip to content

Commit 1a22122

Browse files
committed
Mustache comments.
1 parent b6ee140 commit 1a22122

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

runprompt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ def render_template(template, variables):
120120
return ctx
121121

122122
def render(tmpl, ctx):
123+
# Remove comments: {{! ... }}
124+
tmpl = re.sub(r"\{\{!.*?\}\}", "", tmpl, flags=re.DOTALL)
125+
123126
# Process sections: {{#key}}...{{/key}}
124127
def section_replace(match):
125128
key = match.group(1)

tests/test-mustache.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,23 @@ def test_combined():
8585
{"items": [1]}, "have")
8686

8787

88+
def test_comments():
89+
print("\n--- Comments ---")
90+
test("simple comment", "Hello {{! this is a comment }}World", {}, "Hello World")
91+
test("comment removes entirely", "{{! comment }}", {}, "")
92+
test("comment with variable", "{{! ignore }}{{name}}", {"name": "Alice"}, "Alice")
93+
test("multiline comment", "Hello {{! this\nis\nmultiline }}World", {}, "Hello World")
94+
test("comment between variables", "{{a}}{{! middle }}{{b}}", {"a": "X", "b": "Y"}, "XY")
95+
96+
8897
def main():
8998
test_basic_interpolation()
9099
test_dot_notation()
91100
test_sections()
92101
test_section_lists()
93102
test_inverted_sections()
94103
test_combined()
104+
test_comments()
95105

96106
print("\n" + "=" * 40)
97107
print("Passed: %d, Failed: %d" % (passed, failed))

0 commit comments

Comments
 (0)