Last Updated: February 25, 2016
·
1.587K
· bennylope

Clean up your Django/Jinja2/Liquid 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.