Skip to content

Creating a NativeTemplate directly doesn't create a NativeEnvironment #1091

@cindyzwang

Description

@cindyzwang

Expected Behavior

Create a dictionary with keys and values that are strings. I was able to do this correctly when I was doing NativeTemplate(...).render(...) so I would expect that the behavior would be the same if I used a NativeEnvironment(...).from_string(...).render(...).

Actual Behavior

I have some templates that create dictionaries. However, those tests error out because I expect to create a payload like {'apples': 'bananas'} but am instead creating {'apples: bananas'}.

I dug around and saw that preserve_quotes=False here, which was odd because I thought the behavior should be the same if I was creating an environment or using the spontaneous environment. But it turns out the spontaneous environment is the base Environment.

Template Code

>>> env = NativeEnvironment()
>>> env.from_string('{ "{{a}}": "{{b}}" }').render(a='apple', b='banana')
{'apple: banana'}
>>> NativeTemplate('{ "{{a}}": "{{b}}" }').render(a='apple', b='banana')
{'apple': 'banana'}
>>> NativeTemplate('').environment
<jinja2.environment.Environment object at 0x10a688cc0>

Full Traceback

No traceback because it doesn't throw an error, this is just not the expected behavior

Your Environment

  • Python version: 3.6.6
  • Jinja version: 2.10

How I'm currently working around it

env = Environment()
... # set up globals & filters
env.template_class = NativeTemplate

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions