2010
What’s the Deal With: Django Template Indentation
I like my source code to look good (if at all possible). Django definitely helps with this but you are essentially in a 0 sum game. You make your template code look good or your output HTML source look good, not both it seems.
OK, so in the docs you will first of all come across something like this:
{% for o in some_list %}
<li class="{% cycle 'row1' 'row2' %}">
...
</li>
{% endfor %}
The template tags start with 0 indentation, in other words at the current level of indentation. Everything inside of those tags are then indented by one level, and normal HTML indentation practices resume.
But then, as soon as you find template tags within HTML in the docs they start to ignore HTML indentation:
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% endfor %}
</ul>
Every natural instinct makes me want to write:
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% endfor %}
</ul>
So what is the deal here? I understand that the output HTML will have two levels of indentation between the ul and the li elements:
<ul>
<li>Marcus</li>
<li>Chris</li>
<li>Rob</li>
</ul>
But if we really cared about that we would not indent anything inside a template tag which had 0 indentation already.
No One Does It
I have never seen a good solution to creating dynamic pages with nice looking source code, how hard would it be to add a layer in the template building phase to remove traces of the existence of template tags?
Say for example removing a level of indentation when a nested template tag is used, or removing newlines when a whole bunch of template tags are encountered in a contiguous block.
Ah well. I guess dynamic websites will just be plagued with weird looking source code for ever.