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 are not accessible within imports w/ enable_async #1494

Closed
daniel-stoneuk opened this issue Sep 18, 2021 · 4 comments · Fixed by #1495
Closed

Template globals are not accessible within imports w/ enable_async #1494

daniel-stoneuk opened this issue Sep 18, 2021 · 4 comments · Fixed by #1495
Milestone

Comments

@daniel-stoneuk
Copy link
Contributor

daniel-stoneuk commented Sep 18, 2021

Template globals are not applied correctly when using the enable_async environment flag.

When used normally, template globals can be used within imported macros.

With enable_async=True, template globals can only be accessed in macros within the immediate template.

Example script:

from jinja2 import Environment
from jinja2.loaders import DictLoader
import asyncio

templates = {
    "child.html": """\
{% extends default_layout or 'default.html' %}
{% from 'helpers.html' import imported_get_value%}
{% macro get_value() %}{{value}}{% endmacro %}
]{% block body %}
    {{ get_value() }}
    {{ imported_get_value() }}
{% endblock %}
""",
    "default.html": """\
<!doctype html>
<title>{{ value }}</title>
{% block body %}{% endblock %}
""",
    "helpers.html": """\
{% macro imported_get_value() %}{{value}}{% endmacro %}
""",
}


def sync_main():
    env = Environment(loader=DictLoader(templates))
    tmpl = env.get_template("child.html", globals={"value": "hello"})
    print(tmpl.render())


async def async_main():
    env = Environment(loader=DictLoader(templates), enable_async=True)
    tmpl = env.get_template("child.html", globals={"value": "hello"})
    print(await tmpl.render_async())


if __name__ == "__main__":
    print("---- SYNC ----")
    sync_main()
    print("---- ASYNC ----")
    asyncio.run(async_main())

Output

---- SYNC ----
<!doctype html>
<title>hello</title>

    hello
    hello

---- ASYNC ----
<!doctype html>
<title>hello</title>

    hello
    

I expected the above output to be the same for sync and async.

Environment:

  • Python version: 3.9.6
  • Jinja version: 3.0.1
@daniel-stoneuk daniel-stoneuk changed the title Template globals are not accessible within imports, w/ enable_async Template globals are not accessible within imports w/ enable_async Sep 18, 2021
@daniel-stoneuk
Copy link
Contributor Author

Apologies, I accidentally pressed enter and posted this too soon. I will edit the original comment with more information shortly.

@daniel-stoneuk
Copy link
Contributor Author

I've had a look into the code to see if I can determine why this is happening, and I have identified the following line that stops the vars from being passed to the new context.

self.write("_get_default_module_async()")

Changing this to self.write("_get_default_module_async(context)") resolves this problem, however, I am unsure if this will cause issues elsewhere.

pytest is still passing after making this change.

@davidism
Copy link
Member

Happy to review a PR

daniel-stoneuk added a commit to daniel-stoneuk/jinja that referenced this issue Sep 18, 2021
* Copied tests from test_imports.py
* Removed unused fixture in import tests
@daniel-stoneuk
Copy link
Contributor Author

Cheers, hopefully the PR should resolve this!

@davidism davidism linked a pull request Sep 26, 2021 that will close this issue
6 tasks
@davidism davidism added this to the 3.0.2 milestone Oct 4, 2021
@davidism davidism closed this as completed Oct 4, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants