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

2.9 template optimizer seems to apply filters too early. #739

Closed
mjpieters opened this issue Jul 7, 2017 · 1 comment
Closed

2.9 template optimizer seems to apply filters too early. #739

mjpieters opened this issue Jul 7, 2017 · 1 comment
Labels

Comments

@mjpieters
Copy link

A {{ [1, 2, 3] | random }} interpolation results in one value being picked and re-used, no matter how often that interpolation is run inside a loop:

$ cat test.py
from jinja2 import Template
template = Template('''
{%- for i in range(10) -%}
{{ ['foo', 'bar', 'baz', 'eggs', 'ham', 'spam'] | random }}{{ ' ' }}
{%- endfor %}
''')
print(template.render())
$ python3 test.py
baz baz baz baz baz baz baz baz baz baz

This started with 2.9 and is present in 2.9.6 still. 2.8.1 doesn't show this behaviour (downgrading with pip install -U Jinja2==2.8.1 results in a proper random series of choices).

When you make the list a variable, things work as expected as well, so {% set seq = [...] %} then {{ seq | random }} works fine, as well as passing in the sequence as a template variable on rendering.

@ThiefMaster ThiefMaster added the bug label Jul 7, 2017
@davidism
Copy link
Member

davidism commented Jul 7, 2017

Fixed by #734 by making random a context filter. If I have time I'll go back and figure out what changed between 2.8 and 2.9.

@davidism davidism closed this as completed Jul 7, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants