mitsuhiko / minijinja

MiniJinja is a powerful but minimal dependency template engine for Rust compatible with Jinja/Jinja2
https://docs.rs/minijinja/
Apache License 2.0
1.67k stars 101 forks source link

Context has Undefined value if there is the underscore sign in it #196

Closed user-name-name closed 1 year ago

user-name-name commented 1 year ago

Description

When using the python's binding, the context has "Undefined" value if there is the underscore sign in the context's object.

Reproduction steps

ctx = {"ctx": {"_fa": {"a": 1}, "fa": {"a": 2}}}

print(minijinja.render_str('string = {{ ctx["fa"]["a"] }}', **ctx))  # works
print(minijinja.render_str('string = {{ ctx["_fa"]["a"] }}', **ctx))  # fails

The error is

   1 > string = {{ ctx["_fa"]["a"] }}
     i                ^^^^^^^^^^^^ undefined value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Referenced variables: {
    ctx: {
        "_fa": Undefined,
        "fa": {
            "a": 2,
        },
    },
}

Additional helpful information:

What did you expect

Two examples should work without errors.

mitsuhiko commented 1 year ago

That wasn't intentional for dictionaries. Fixed it. It's intentional for objects though so you can hide internals and dunder methods.

user-name-name commented 1 year ago

Sorry for lobbying, I asked my collegues to vote but didn't expect they all would do this 😆