None

Django Letter Headings

March 13, 2010

Here's some code, taken from a Django template, that will output a letter header each time the name of the piece changes to start with a different letter. This assumes that the piece names are in alphabetical order.

<ul id="pieces" title="Pieces" selected="true">
{% for piece in Pieces %} 
  {% ifchanged piece.name|slice:":1" %}<li class="group">{{piece.name|slice:":1"}}</li>{%endifchanged%}
  <li><a href="/pieces/{{piece.slug}}/">{{piece.name}}</a></li>
{% endfor %}
</ul>