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

can't use enviroment variable #31

Closed
wifasoi opened this issue Jan 6, 2017 · 4 comments
Closed

can't use enviroment variable #31

wifasoi opened this issue Jan 6, 2017 · 4 comments

Comments

@wifasoi
Copy link

wifasoi commented Jan 6, 2017

I noted i can't use the enviroment variable in my template [commit: 4b6c048]. I made some testing with this template:
[export TEST=succes]
{% if environ.TEST is defined %}TEST={{ environ.TEST }}{% endif %}

I pin-poin the issue at this line:
env.globals['environ'] = os.environ.get
I'm not an expert with jinja2, but i think you need to pass a dictionary (I noticed an iterable is fine) and not a fuction.
I fixed by changing the line with:
env.globals['environ'] = dict(os.environ)
or (becouse os.environ is iterable)
env.globals['environ'] = os.environ

I think the functionality need to be documented in the README.

@mattrobenolt
Copy link
Owner

The intended usage is:

{{ environ('FOO') }}
{{ environ('FOO', 'somedefault') }}

So the behavior I have now is correct. This is the same as doing os.environ.get('foo', bar')

@mattrobenolt
Copy link
Owner

And yeah, I didn't realize this wasn't documented. Should definitely add that.

@wifasoi
Copy link
Author

wifasoi commented Jan 9, 2017

(sorry if i bother you.. but just for the records)
This is down to choice in the program behavior, but with your approach you can't use the "is define" statement. A possible work-around is to do the followings:
{% if environ('FOO', False) %}

@mattrobenolt
Copy link
Owner

@wifasoi I'd do this:

{% if environ('FOO') is none %}
it doesn't exist
{% else %}
{{ environ('FOO') %}
{% elseif %}

So instead of is defined you can just as easily use is none.

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

Successfully merging a pull request may close this issue.

2 participants