Skip to content
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

Open
clarkwang opened this issue Feb 20, 2024 · 0 comments

Comments

@clarkwang
Copy link

clarkwang commented Feb 20, 2024

I've come up with the following minimal repro steps.

Python script:

$ cat test.py
#!/usr/bin/env python3

import jinja2 as j2
import os, sys

print(f'jinja2 version: {j2.__version__}')

env = j2.Environment(loader=j2.FileSystemLoader('.') )
tmpl = env.get_template(sys.argv[1], globals={ 'ENV': os.getenv })
print(tmpl.render() )

Data files:

$ cat common
{% set var1 = ENV('HOME') %}
$ cat file1
{% include 'file2' %}
$ cat file2
{% import 'common' as COMMON %}
{{ COMMON.var1 }}

To repro the issue:

$ python3 test.py file2
jinja2 version: 3.1.3

/root
$ python3 test.py file1
jinja2 version: 3.1.3
Traceback (most recent call last):
  File "/root/void/python/jinja/t1/test.py", line 10, in <module>
    print(tmpl.render() )
          ^^^^^^^^^^^^^
  File "/usr/local/py3/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/py3/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "file1", line 1, in top-level template code
    {% include 'file2' %}
    ^^^^^^^^^^^^^^^^^^^^^^
  File "file2", line 1, in top-level template code
    {% import 'common' as COMMON %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/py3/lib/python3.11/site-packages/jinja2/environment.py", line 1405, in make_module
    return TemplateModule(self, ctx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/py3/lib/python3.11/site-packages/jinja2/environment.py", line 1535, in __init__
    body_stream = list(template.root_render_func(context))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "common", line 1, in top-level template code
    {% set var1 = ENV('HOME') %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/py3/lib/python3.11/site-packages/jinja2/utils.py", line 83, in from_obj
    if hasattr(obj, "jinja_pass_arg"):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'ENV' is undefined

As we can see, python3 test.py file2 works but python3 test.py file1 fails.

@davidism davidism changed the title [v3.x] Template globals not visible in {% import %}'ed file when it's {% include %}'ed by another file Template globals not visible in {% import %}'ed file when it's {% include %}'ed by another file Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant