Skip to content

Commit

Permalink
Fixed django#14831 -- Extended template style guide in docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancheley authored and felixxm committed Mar 11, 2024
1 parent 7326513 commit f2c3524
Showing 1 changed file with 133 additions and 4 deletions.
137 changes: 133 additions & 4 deletions docs/internals/contributing/writing-code/coding-style.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,149 @@ Imports
Template style
==============

* In Django template code, put one (and only one) space between the curly
brackets and the tag contents.
Follow the below rules in Django template code.

* ``{% extends %}`` should be the first non-comment line.

Do this:

.. code-block:: html+django

{% extends "base.html" %}

{% block content %}
<h1 class="font-semibold text-xl">
{{ pages.title }}
</h1>
{% endblock content %}

Or this:

.. code-block:: html+django

{# This is a comment #}
{% extends "base.html" %}

{% block content %}
<h1 class="font-semibold text-xl">
{{ pages.title }}
</h1>
{% endblock content %}

Don't do this:

.. code-block:: html+django

{% load i18n %}
{% extends "base.html" %}

{% block content %}
<h1 class="font-semibold text-xl">
{{ pages.title }}
</h1>
{% endblock content %}

* Put exactly one space between ``{{``, variable contents, and ``}}``.

Do this:

.. code-block:: html+django

{{ user }}

Don't do this:

.. code-block:: html+django

{{user}}

* In ``{% load ... %}``, list libraries in alphabetical order.

Do this:

.. code-block:: html+django

{% load i18n l10 tz %}

Don't do this:

.. code-block:: html+django

{% load l10 i18n tz %}

* Put exactly one space between ``{%``, tag contents, and ``%}``.

Do this:

.. code-block:: html+django

{{ foo }}
{% load humanize %}

Don't do this:

.. code-block:: html+django

{{foo}}
{%load humanize%}

* Put the ``{% block %}`` tag name in the ``{% endblock %}`` tag if it is not
on the same line.

Do this:

.. code-block:: html+django

{% block header %}

Code goes here

{% endblock header %}

Don't do this:

.. code-block:: html+django

{% block header %}

Code goes here

{% endblock %}

* Inside curly braces, separate tokens by single spaces, except for around the
``.`` for attribute access and the ``|`` for a filter.

Do this:

.. code-block:: html+django

{% if user.name|lower == "admin" %}

Don't do this:

.. code-block:: html+django

{% if user . name | lower == "admin" %}

{{ user.name | upper }}

* Within a template using ``{% extends %}``, avoid indenting top-level
``{% block %}`` tags.

Do this:

.. code-block:: html+django

{% extends "base.html" %}

{% block content %}

Don't do this:

.. code-block:: html+django

{% extends "base.html" %}

{% block content %}
...

View style
==========
Expand Down

0 comments on commit f2c3524

Please sign in to comment.