Skip to content

Template function defined() doesn't work with dictionaries? #659

Open
@mjordan

Description

@mjordan

I'm running 0.12.7 and have hit a problem. I'm wondering if anyone can explain why the following code doesn't work if my_dict['id'] is defined in the current template:

% if defined(my_dict['id']):
    <input type="hidden" name="id" value="{{my_dict['id']}}" />
% end    

defined() is not returning True. I have confirmed that my_dict['id'] is getting passed to my template because I can output it in textfields in my form. If I replace my_dict['id'] with the name of a string variable, defined() does return True and the HTML is rendered as expected.

Possible bug, working as designed, or am I misunderstanding something?

Activity

avelino

avelino commented on Sep 17, 2014

@avelino
Member

defined read var, example:

defined('abc')

Recommend return you dict via args, example:

template('test.html', **my_dict)

In template:

% if defined('id'):
    <input type="hidden" name="id" value="{{ id }}" />
% end    

http://bottlepy.org/docs/dev/stpl.html#stpl.defined

mjordan

mjordan commented on Sep 17, 2014

@mjordan
ContributorAuthor

Thanks a million, that worked. But the docs at http://bottlepy.org/docs/0.12/tutorial.html#templates and http://bottlepy.org/docs/0.12/stpl.html don't provide examples of this type of usage. I don't mind opening a pull request to update the docs.

avelino

avelino commented on Sep 17, 2014

@avelino
Member

All contributions are welcome!

added a commit that references this issue on Sep 18, 2014
eric-wieser

eric-wieser commented on Sep 23, 2014

@eric-wieser
Contributor

Not sure that docs change makes sense - you're documenting a python language feature, not a bottle feature

mjordan

mjordan commented on Sep 24, 2014

@mjordan
ContributorAuthor

I'm documenting how to use a python language feature in a bottle template. Not sure why that doesn't make sense.

added a commit that references this issue on Oct 22, 2014

Merge pull request #660 from mjordan/master

eric-wieser

eric-wieser commented on Dec 27, 2014

@eric-wieser
Contributor

Answering your original question:

% if 'id' in my_dict:
    <input type="hidden" name="id" value="{{my_dict['id']}}" />
% end
basbebe

basbebe commented on Feb 20, 2015

@basbebe

@eric-wieser thanks, that was very helpful.
I was totally lost since it seems that using the **ars method does not work with namespaces.
My variables come from a .conf ini file with namespaces. However, the separator makes the variables not work.
Using the "regular" approach solved that for me.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Template function defined() doesn't work with dictionaries? · Issue #659 · bottlepy/bottle