Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Changes to jinja environment variables are global, and shouldn't be #67

Open
alextremblay opened this issue Aug 27, 2020 · 2 comments
Open

Comments

@alextremblay
Copy link

If i run the following code:

from pathlib import Path
from yasha.cli import cli

Path('template.j2').write_text("{{ var is string }}, {{ var }}")
cli(['--var=foo', 'template.j2'], standalone_mode=False)
assert Path('template').read_text() == 'True, foo'

cli(['--var=foo', 'template.j2'], standalone_mode=False)
assert Path('template').read_text() == 'True, foo'

everything works fine.
If, instead, i run this code:

from pathlib import Path
from yasha.cli import cli

Path('template.j2').write_text("<< var is string >>, << var >>")
Path('extensions.py').write_text("""
BLOCK_START_STRING = '<%'
BLOCK_END_STRING = '%>'
VARIABLE_START_STRING = '<<'
VARIABLE_END_STRING = '>>'
COMMENT_START_STRING = '<#'
COMMENT_END_STRING = '#>'
""")

cli(['--var=foo', '-e', 'extensions.py', 'template.j2'], standalone_mode=False)
assert Path('template').read_text() == 'True, foo'

Path('template.j2').write_text("{{ var is string }}, {{ var }}")
cli(['--var=foo', 'template.j2'], standalone_mode=False)
assert Path('template').read_text() == 'True, foo'  # -> AssertionError
Path('template').read_text()  # -> '{{ var is string }}, {{ var }}'

The second call to cli fails, it prints the entire template, unrendered, to the output file. It appears jinja no longer recognizes that second template as a template, and just treats it as text. I suspect that although the second cli call doesn't use the extensions file, the jinja configuration overrides in that file are persisting somewhere in the python interpreter's global state

@alextremblay
Copy link
Author

I think i found the culprit, in yasha/cli.py

@kblomqvist
Copy link
Owner

I think i found the culprit, in yasha/cli.py

The first cli call should print the template also but it seems like the Jinja defaults (module global variables) didn't get change in time. This feature is related to https://github.com/kblomqvist/yasha#template-syntax.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants