You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the following code the result depends on atoescaping even though there are no characters which need escaping. Am I understanding atoescaping incorrectly?
If you set autoescape=False it works as expected.
There is a workaround (inspired by #486). I consider it a bug as it is exactly what the OP of #486 wanted to achieve from filtering.
The goal is to process (with a filter) whatever is inside the block assignment and store it to a variable. The filter is responsible for parsing the content and coercing it to a correct type (bool, number, string, lists). Note that the content of the block assignment can contain any Jinja instructions.
Expected Behavior
Prints POSTGRES
Actual Behavior
Prints ORACLE
Template Code
fromjinja2importEnvironment, FileSystemLoaderenv=Environment(loader=FileSystemLoader("."), autoescape=True)
env.filters['parse_bool'] =lambdas: s=='True'# BUGtemplate=env.from_string("""{% set ORACLE | parse_bool %}False{% endset %}{{ 'ORACLE' if ORACLE else 'POSTGRES' }}""")
print(template.render())
# WORKAROUNDtemplate=env.from_string("""{% set ORACLE %}False{% endset %}{% set ORACLE = ORACLE | parse_bool %}{{ 'ORACLE' if ORACLE else 'POSTGRES' }}""")
print(template.render())
Your Environment
Python version: 3.8
Jinja version: 2.11.2
The text was updated successfully, but these errors were encountered:
I dug into what exactly is happening there, this is the code that jinja generates for the buggy template:
(I stripped some whitespace, my template looks like this: "{% set ORACLE | parse_bool %}False{% endset %}{{ 'ORACLE' if ORACLE else 'POSTGRES' }}")
The issue is the quite obvious - the "False" string is converted to markup, then to False by the filter, but then back to markup because autoescape is True. This points to that this is related to #490 . The markup calls are inserted by compiler here in visit_Filter and here in visit_AssignBlock
This needs to be solved by someone with deeper understanding of the Safe/Unsafe interactions in jinja, i.e. not me.
In the following code the result depends on atoescaping even though there are no characters which need escaping. Am I understanding atoescaping incorrectly?
If you set
autoescape=False
it works as expected.There is a workaround (inspired by #486). I consider it a bug as it is exactly what the OP of #486 wanted to achieve from filtering.
The goal is to process (with a filter) whatever is inside the block assignment and store it to a variable. The filter is responsible for parsing the content and coercing it to a correct type (bool, number, string, lists). Note that the content of the block assignment can contain any Jinja instructions.
Expected Behavior
Prints POSTGRES
Actual Behavior
Prints ORACLE
Template Code
Your Environment
The text was updated successfully, but these errors were encountered: