106

I have a python dictionary:

settings = {
   "foo" : "baz",
   "hello" : "world"
}

This variable settings is then available in the Jinja2 template.

I want to check if a key myProperty exists in the settings dict within my template, and if so take some action:

{% if settings.hasKey(myProperty) %}
   takeSomeAction();
{% endif %}

What is the equivalent of hasKey that I can use?

2
  • Just check: if settings[myproperty] is not None: I think this should work. Commented Jan 2, 2015 at 9:50
  • 1
    or if settings.myproperty is defined: Commented Jan 2, 2015 at 9:57

4 Answers 4

200

Like Mihai and karelv have noted, this works:

{% if 'blabla' in item %}
  ...
{% endif %}

I get a 'dict object' has no attribute 'blabla' if I use {% if item.blabla %} and item does not contain a blabla key

Sign up to request clarification or add additional context in comments.

4 Comments

Same for me. In my particular case I'm using Jinja2 inside Ansible.
Using jinja2 inside Ansible. Nothing helps. Both {% if 'blabla' in item %} and {% if item.blabla %} return the same 'dict object' has no attribute 'blabla' wtf???
argument of type 'StringField' is not iterable. Does not work for me
Is item supposed to be a dictionary in this answer? That's a confusing name (an item is a key/value pair inside a dictionary). Sorry if I'm missing something.
44

You can test for key definition this way:

{% if settings.property is defined %}

#...
{% endif %}

3 Comments

This looks like the correct way of checking if a key is present in dict or not. +1
Your answer checks for object attributes, not dict keys.
@Samha' - Dict objects can be accessed via the dot operator in Jinja2. While this appears to be accessing a property - it's a perfectly valid solution for testing for a dict key.
15

This works fine doesn't work in cases involving dictionaries. In those cases, please see the answer by tshalif. Otherwise, with SaltStack (for example), you will get this error:

Unable to manage file: Jinja variable 'dict object' has no attribute '[attributeName]'

if you use this approach:

{% if settings.myProperty %}

note:
Will also skip, if settings.myProperty exists, but is evaluated as False (e.g. settings.myProperty = 0).

9 Comments

Can myProperty be a variable in the approach above?
Yes, a key in a dictionary. Is the equivalent of hasKey. And btw, hasKey is deprecated. Use in instead
when setting.myProperty exists, but it is equal to zero (integer 0) or to the boolean value False, this if test not for 'hasKey'. This means you need: {% if 'myProperty' in settings %}
this doesn't work for me (at least working with ansible). i found i needed {% if settings.myProperty is defined %}.
I don't get why this is accepted as answer - it throws an error if the dict is missing the key. Please correct the answer.
|
1

Actually, in the style of Python, if you do a simple if statement, it will work:

{% if settings.foo %}
Setting Foo: {{ settings.foo }}
{% endif %}
{% if settings.bar %}
Setting Bar: {{ settings.bar }}
{% endif %}
{% if settings.hello %}
Setting Hello: {{ settings.hello }}
{% endif %}

Output:

Setting Foo: baz
Setting Hello: world

Cheers!

1 Comment

This does not work to check if a key is set to false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.