Fix template variable shadowing built-in function names breaking templates #1359
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixed a critical issue where template variables that shadow Python built-in function names (like
dir
,span
,object
, etc.) were being corrupted during formatting, causing templates to break.Problem
When formatting Jinja templates, variables that happened to match Python built-in names were being removed:
This made templates non-functional as the variable reference was completely removed.
Root Cause
The issue was in
src/djlint/formatter/indent.py
in theformat_data
function. When processing{% set %}
tags, djLint was:eval()
on template variablesdir
,eval("dir")
returned the built-indir
function objectSolution
Modified the
format_data
function to be more conservative about what it evaluates:Additional Protection
Added
inside_template_block()
checks incompress.py
andattributes.py
to prevent HTML tag processing from affecting template content.Testing
Added comprehensive test cases covering:
dir
,span
,button
)object
,dir
)The fix is minimal, surgical, and maintains backward compatibility while preventing the dangerous evaluation of template variables.
Fixes #828.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.