Fix Jinja2 template errors from invalid parameter/block names with special characters (SKY-7356) (#4644)
This commit is contained in:
@@ -7,6 +7,28 @@ class Constants:
|
||||
MissingVariablePattern = var_pattern = r"\{\{\s*([a-zA-Z_][a-zA-Z0-9_.\[\]'\"]*)\s*\}\}"
|
||||
|
||||
|
||||
def replace_jinja_reference(text: str, old_key: str, new_key: str) -> str:
|
||||
"""Replaces jinja-style references in a string.
|
||||
|
||||
Handles patterns like {{oldKey}}, {{oldKey.field}}, {{oldKey | filter}}, {{oldKey[0]}}
|
||||
|
||||
Args:
|
||||
text: The text to search in
|
||||
old_key: The key to replace (without braces)
|
||||
new_key: The new key to use (without braces)
|
||||
|
||||
Returns:
|
||||
The text with references replaced
|
||||
"""
|
||||
# Match {{oldKey}} or {{oldKey.something}} or {{oldKey | filter}} or {{oldKey[0]}} etc.
|
||||
# Use negative lookahead to ensure key is not followed by identifier characters,
|
||||
# which prevents matching {{keyOther}} when searching for {{key}}
|
||||
# Capture whitespace after {{ to preserve formatting (e.g., "{{ key }}" stays "{{ newKey }}")
|
||||
escaped_old_key = re.escape(old_key)
|
||||
pattern = rf"\{{\{{(\s*){escaped_old_key}(?![a-zA-Z0-9_])"
|
||||
return re.sub(pattern, rf"{{{{\1{new_key}", text)
|
||||
|
||||
|
||||
def get_missing_variables(template_source: str, template_data: dict) -> set[str]:
|
||||
# quick check - catch top-level undefineds
|
||||
env = Environment(undefined=StrictUndefined)
|
||||
|
||||
Reference in New Issue
Block a user