-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Template globals not visible in {% import %}'ed file when it's {% include %}'ed by another file #1939
Comments
@clarkwang The error occurs because #!/usr/bin/env python3
import jinja2 as j2
import os
import sys
print(f'jinja2 version: {j2.__version__}')
# Set up Jinja2 environment and add the global function
env = j2.Environment(loader=j2.FileSystemLoader('.'))
env.globals['ENV'] = os.getenv
# Load the template specified by the first command line argument
tmpl = env.get_template(sys.argv[1])
# Render the template
print(tmpl.render())
|
@j7an adding globals to a template is supported: https://jinja.palletsprojects.com/en/3.1.x/api/#the-global-namespace. That said, you're correct that they're not setting the globals in the correct place. In their example, they set the globals on |
I've come up with the following minimal repro steps.
Python script:
Data files:
To repro the issue:
As we can see,
python3 test.py file2
works butpython3 test.py file1
fails.The text was updated successfully, but these errors were encountered: