I'm trying to generate a list akin to:
<ul>
<li>Parent 1
<ul>
<li>Child 1</li>
<li>Child 2</li>
<li class="last">Child 3</li>
</ul>
</li>
<li>Parent 2
<ul>
<li>Child 1</li>
<li>Child 2</li>
<li class="last">Child 3</li>
</ul>
</li>
<li class="last">Parent 3
<ul>
<li>Child 1</li>
<li>Child 2</li>
<li class="last">Child 3</li>
</ul>
</li>
</ul>
I originally just did:
<ul>
{% recursetree mytree %}
<li class="{% if not node.get_next_sibling %}last{% endif %}">
{{ node }}
{% if not node.is_leaf_node %}
<ul>
{{ children }}
</ul>
{% endif %}
</li>
</ul>
However, the call to node.get_next_sibling
results in an extra query for each item. Obviously, that's not ideal. So I tried using tree_info
and structure.closed_levels
to determine the last item:
{% for node,structure in mytree|tree_info %}
{% if structure.new_level %}<ul><li>{% else %}</li><li>{% endif %}
<li class="{% if structure.closed_levels|length > 0 %}last{% endif %}">
{{ node }}
{% for level in structure.closed_levels %}</li></ul>{% endfor %}
{% endfor %}
This works great, except the last root level item does not get the "last" class, because its structure.closed_levels
is always an empty list. (It really only works for child items).
I'm sure I'm not the first to need to accomplish something similar, so I'm hoping someone here might have a solution already.