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

is defined and is none are not working for environment variables #68

Closed
jaydrogers opened this issue Nov 15, 2018 · 4 comments
Closed

Comments

@jaydrogers
Copy link

jaydrogers commented Nov 15, 2018

Hello,

Thank you for putting this handy tool together! Today I noticed that some of my blocks are displaying even though my environment variables are not defined.

My environment:

Output of jinja2 --version:

jinja2-cli v0.6.0
 - Jinja2 v2.10

Here is my test.j2:

With is defined set:

{% if environ('DEFINED_VARIABLE') is defined %}
This should display.
{% endif %}

{% if environ('NOT_DEFINED_ABCD1234') is defined %}
This should NOT display.
{% endif %}

I even attempted to follow this #31 (comment) and set it to is none:

{% if environ('DEFINED_VARIABLE') is defined %}
This should display.
{% endif %}

{% if environ('NOT_DEFINED_ABCD1234') is none %}
This should NOT display.
{% endif %}

Command I am executing:

export DEFINED_VARIABLE=Hello
jinja2 test.j2 > test.txt

Expected results of test.txt:

This should display.

Actual results of test.txt:

This should display.

This should NOT display.

Any thoughts would be appreciated!

@mattrobenolt
Copy link
Owner

I just tested locally and it works fine with is none.

$ cat foo.j2
{% if environ('TEST') is none %}nope{% else %}{{ environ('TEST') }}{% endif %}
$ jinja2 foo.j2
nope
$ TEST=hello jinja2 foo.j2
hello

Can you try against the master branch maybe? I haven't tagged a release in a bit ( not sure why ), and maybe something changed on master related to this? I don't personally use this feature, but can confirm in my testing right now that it behaves as I'd expect.

@jaydrogers
Copy link
Author

Follow up update on this... I noticed that if I set it to is not none I get my expected results. See this new test:

test.j2

{% if environ('DEFINED_VARIABLE') is defined %}
This should display.
{% endif %}

{% if environ('NOT_DEFINED_ABCD1234') is not none %}
This should NOT display.
{% endif %}

Command I am executing:

export DEFINED_VARIABLE=Hello
jinja2 test.j2 > test.txt

Results:

This should display.

Suggested change

It's great that this works but the commands seem very confusing. My condition of is not none is displaying when it isn't defined.

Any chance that it can just support is defined? I think that would make this a lot clearer.

@mattrobenolt
Copy link
Owner

I can't support is defined since that's a very different meaning in Jinja. I can't replicate that with a function afaik. The function environ() is literally a wrapper to os.environ which will return None or the value. What's wrong with is not none ? That works just fine. This is literally what you'd write in Python.

You can also just do {% if environ('FOO') %} instead if you want to make it shorter, and don't care about a variable existing that may be an empty string. I think this behavior would be pretty expected since it's common to unset a variable by doing something like FOO= jinja2 foo.j2. In my scripts, I normally just use {% if environ('FOO') %} or {% if not environ('FOO') %}.

@jaydrogers
Copy link
Author

Thanks for the explanation! Oh boy... I apologize for my lack of understanding. I don't know much Python at all and I am coming from an Ansible background. That's were I got my is defined experience from...

After your explanation, and your suggestion to use {% if environ('FOO') %}, this makes complete sense.

Sorry for the confusion. My brain was operating like mashed potatoes and I was writing code for too long 😀. I had a battle like this going on in my head: https://www.youtube.com/watch?v=HtTZ43jCfKQ

Cheers @mattrobenolt! Thanks for maintaining this project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants