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|slice:":1" %}<li class="group">{{|slice:":1"}}</li>{%endifchanged%}
  <li><a href="/pieces/{{piece.slug}}/">{{}}</a></li>
{% endfor %}