Leveraging Holistic Synergies

Cleaning Django template tags

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 2014-02-24