Ticket 14831 has the following details:
It would be nice to have a standard template style that should be followed. For example, I like to indent my Django templates by two additional spaces every time I enter a new HTML or template block. The Django core templates vary greatly in regards to style and indentation. Much of the time template readability is sacrificed in favour of attempting to preserve the “appearance” of the generated HTML code.
Regardless of what is decided, it would be great to have at least some guidelines for templates added to http://docs.djangoproject.com/en/dev/internals/contributing/#coding-style
The guidance on moving the ticket forward from @nessita was:
- An overview of the various styles found in the current templates,
- A draft patch based on whatever the most common/best practice seems to be,
- A thread on the Django Forum to let folks debate the options.
I’ve not done anything (yet) for point (2) above, but I have reviewed the various styles that seem to be in place (1) and am now posting here to let folks debate the options (3)
My findings are (and can be found in more detail in comments 10 through 17:
- The usage of
{{
is generally followed by a single space (inline javascript seems to be the exception) - The usage of
{%
is always followed by a single space (the one exception I was able to find was related to a jinja2 whitespace control in./forms/jinja2/django/forms/widgets/multiwidget.html
{% extends %}
is always at the top ofhtml
files{% load %}
is nearly always at the top. There are two exceptions.
{% block %}
The following ‘standards’ are used:
- the <link ...>
tends to be on a single line, see for example /django/contrib/admin/templates/admin/base.html
Line 6
- Simple logic for html element classes tends to be on a single line, see for example ./docs/_theme/djangodocs/layout.html
Line 80
- Single elements can are on a single line, see for example ./django/contrib/admin/templates/admin/base.html
Line 102
There are two files which may fall outside of the standard:
./django/contrib/admin/templates/admin/auth/user/change_password.html
Line 19./django/contrib/admin/templates/admin/change_form.html
Line 36
In each of those cases there are block elements at the end of the line so these seem more like false positives than an actual issues
Proposal for Standard
Given the findings above I think a standard is mostly already there, but just needs to be documented:
{{
should be followed by a single space{%
should be followed by a single space{% extends %}
should always be on line 1{% load %}
and there should only be 1{% load %}
statement per file{% block %}
probably needs more discussion on what standard should exist
As mentioned above, details of the work are in the comments on the ticket.
I look forward to your feedback