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

Jinja2 NativeEnvironment dropping single quotes between elements in template to render #1020

geauxvirtual opened this issue Jun 18, 2019 · 1 comment · Fixed by #1086


Copy link

@geauxvirtual geauxvirtual commented Jun 18, 2019

Expected Behavior

>>> from jinja2 import Template
>>> template = Template("'{{ foo }}', '{{ bar }}', '{{ baz }}'")
>>> template.render(foo='foobar', bar='barbaz', baz='bazfoo')
('foobar', 'barbaz', 'bazfoo')

Actual Behavior

>>> from jinja2.nativetypes import NativeEnvironment
>>> e = NativeEnvironment()
>>> t = e.from_string("'{{ foo }}', '{{ bar }}', '{{ baz }}'")
>>> t.render(foo='foobar', bar='barbaz', baz='bazfoo')
'foobar, barbaz, bazfoo'

Your Environment

  • Python version: 2.7.5
  • Jinja version: 2.10, 2.10.1, current master

Trying to find an explanation of the on the above. Tested this in an interpreter to narrow it down to just Jinja2, and nothing else that may be calling it, such as Ansible with jinja2_native set to True.

When not using jinja2_native, we get the expected behavior above that the three substitutions occur with single quotes around each value. When jinja2_native is set to true (in the NativeEnvironment as shown above), the inner single quotes in the string are disregarded, and a single string is returned with the three substitutions.

This also occurs even with additional characters between the single quotes:

>>> e = NativeEnvironment()
>>> t = e.from_string("--foo='{{ foo }}' --bar='{{ bar }}'")
>>> t.render(foo='foobar', bar='barbaz')
u"--foo='foobar --bar=barbaz'"

Any help would be appreciated.


This comment has been minimized.

Copy link

@mkrizek mkrizek commented Jul 1, 2019

To add a little note on why this (more or less) happens: the template "--foo='{{ foo }}' --bar='{{ bar }}'" is broken down to the following nodes:

  1. --foo='
  2. {{ foo }}
  3. ' --bar='
  4. {{ bar }}
  5. '"

The problem happens when native_concat processes 3. ' --bar=', literal_eval there sees a string, strips the quotes and returns just --bar= which is correct, just not in this context :)

@davidism any thoughts?

ditekunov added a commit to ditekunov/jinja that referenced this issue Sep 2, 2019
Added workaround for the case, explained in pallets#1020 issue
ditekunov added a commit to ditekunov/jinja that referenced this issue Sep 2, 2019
@davidism davidism added this to the 2.11.0 milestone Oct 22, 2019
@pallets pallets deleted a comment from xkrt Nov 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.