Cleaning Django template tags

Lint-like checking for spacing style in templates

Django templates, which share some common syntax with Jinja2 templates and Liquid templates use curly brace delimiters to designate template context variables and template tags (which perform functions in the code). The variables look like this:

{{ some_variable }}

And tags look like this:

{% some_tag var1 var2 %}

The space between the delimiter and the content is important for readability. And it’s because of that readability that this spacing is a convention.

Sometimes the space gets lost, either from mistyping or mentally porting the style from another language. Aside from violating our nice convention, it makes the template code harder to read, and in dense templates difficult to reason about.

{%for x in list%}{{x}}{%endfor%}

The curly-spacing.sh script in the template-cleaners repository automatically cleans up these mistakes.

{% for x in list %}{{ x }}{% endfor %}

For Django users it also includes a bonus django-url.sh script to upgrade old style url tags in which a URL name is provided without quotes.

Originally published February 2014

Are you a Django developer? Have you found yourself wishing you could reuse the code you write or looking for some guidance on building standalone Django apps?

Check out my upcoming book, Django Standalone Apps: A developer's fieldguide to developing reusable Django applications the first guide written specifically for writing standalone apps.