Reputation: 1995
I have the variable fruits
:
fruits = {
"apple": {},
"banana": {
"params": {
"color": "yellow",
"size" 100
}
}
}
..and a jinja2 template my_stuff.j2
:
In my bag I have:
{% for k,v in fruits.iteritems() %}
- {{ k }}
{% endfor %}
When I render it I get:
In my bag I have:
- apple
- banana
Question: How do I reach this:
In my bag I have:
- apple
- banana color="yellow" size="100"
Upvotes: 1
Views: 1920
Reputation: 2791
In Python you can do it like this:
for key,val in fruits.iteritems():
if val.get('params'):
print key + ' ' + str(' '.join('{}={}'.format(param_key, param_val) for param_key, param_val in val['params'].items()))
else:
print key
And in Jinja
you can do something like this:
{%for key,val in something.iteritems()%}
{%if val.get('params')%}
- {{key}} {%for item in val['params'].items()%} {{item | join ('=') }} {%endfor%}
{%else%}
- {{key}}
{%endif%}
{%endfor%}
Upvotes: 1