Database Reference
In-Depth Information
<dd>
<dd>
{{value.head[0].description}}
</dd>
</dd>
{% endif %}
{% if value.hands %}
<dt>
<dt>
Gloves
</dt>
</dt>
<dd>
<dd>
{{value.hands[0].description}}
</dd>
</dd>
{% endif %}
{% if value.feet %}
<dt>
<dt>
Boots
</dt>
</dt>
<dd>
<dd>
{{value.feet[0].description}}
</dd>
</dd>
{% endif %}
{% if value.body %}
<dt>
<dt>
Body Armor
</dt>
</dt>
<dd><ul>
<dd><ul>
{% for piece in value.body %}
<li>
<li>
piece.description
</li>
</li>
{% endfor %}
</ul></dd>
</ul></dd>
{% endif %}
</dl>
</dl>
</dd>
</dd>
In this case, we want the various
description
fields to be text similar to “+3 wizard's hat.”
The context passed to this template, then, would be of the following form:
{
"head"
: [ {
"id"
:
...
,
"description"
:
"+3 wizard's hat"
} ],
"hands"
: [],
"feet"
: [ {
"id"
:
...
,
"description"
:
"old boots"
} ],
"body"
: [ {
"id"
:
...
,
"description"
:
"wizard's robe"
} ],
}
In order to build up this structure, we'll use the following helper functions:
def
def
get_item_index
(
inventory
):
'''Given an inventory attribute, recursively build up an item
index (including all items contained within other items)
'''
result
=
{}
for
for
item
iin
inventory
:
result
[
item
[
'_id'
]]
=
item
iif
'inventory'
iin
item
:
result
.
update
(
get_item_index
(
item
[
'inventory]))
return
return
result