From 29bf2c08bafb5e7a727ff854684b8f9ceeb14767 Mon Sep 17 00:00:00 2001 From: Noethix55555 <277300782+Noethix55555@users.noreply.github.com> Date: Tue, 16 Jun 2026 23:21:45 -0400 Subject: [PATCH] fix: allow % and } in {%p %}/{%tr %}/{%tc %}/{%r %} expressions patch_xml matched the expression body of these directive tags with [^}%]*, which stops at the first % or }. Tags whose expression contains a modulo (if i % 2 == 0) or a literal brace (set d = {"x":1}) were not stripped of their surrounding /// wrapper, so the literal {%p ...%} reached Jinja and raised TemplateSyntaxError: Encountered unknown tag 'p'. Match up to the closing %} or }} instead of excluding the characters. --- docxtpl/template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docxtpl/template.py b/docxtpl/template.py index f20280a..7886480 100644 --- a/docxtpl/template.py +++ b/docxtpl/template.py @@ -183,7 +183,7 @@ def cellbg(m): # by {% xxx %} or {{ xx }} without any surrounding tags : # This is mandatory to have jinja2 generating correct xml code pat = ( - r"](?:(?!]).)*({%%|{{)%(y)s ([^}%%]*(?:%%}|}})).*?" + r"](?:(?!]).)*({%%|{{)%(y)s ((?:(?!%%}|}}).)*(?:%%}|}})).*?" % {"y": y} ) src_xml = re.sub(pat, r"\1 \2", src_xml, flags=re.DOTALL)