14

Is there a tidy-like tool or a tidy configuration, which works fine with Jinja2 templates? The default tidy has problems with Jinja code in attributes and Jinja-loops are formated in an unreadable way. My main requirement is to get the indentation level right. Everything else is nice to have, but not required.

Achim
  • 15,415
  • 15
  • 80
  • 144

3 Answers3

4

You probably want a standalone tool and your question is old, so you've probably already solved it, but just in case this could be helpful. Some editors can handle jinja indent. For example vim with https://github.com/Glench/Vim-Jinja2-Syntax plugin. The '=' command fixes indent. So 'gg=G' fixes indent on all lines. gg to go to the first character; = to fix indent; and G means to the end.

Mark Walker
  • 279
  • 2
  • 6
2

While it is not a standalone tool like HTML Tidy, the atom-beautify package for the Atom text editor works fine for me. I use atom-beautify when developing Flask/Jinja2 applications.

Nimrod
  • 21
  • 3
1

Standalone

A standalone (Jinja2 lint like) tool can be found here:

https://github.com/ramonsaraiva/jinjaninja

Install:

$ pip install jinjaninja

Usage:

$ jinja-ninja templates/header.html 

Output example:

templates/header.html:8:68 Block closures should also have names `{% endblock %}`

Check jinjaninja@github for more information

Pre-Commit

If as part of your local git setup you use pre-commit, check the

Django Templates

If you use Django Templates, I suggest to use django-extensions and make use of: validate_templates

You can use pip to install django-extensions for usage:

 $ pip install django-extensions

Django project settings.py file.:

INSTALLED_APPS = [ ... 
     'django_extensions', 
]

The next time you invoke ./manage.py help you should be able to see all the newly available commands. One is called "validate_templates"

$ python manage.py validate_templates

Visual Code Extension

Visual Code offers two Jinja extensions, the default is "Jinja" but a newer one with more functionality is:

Peter Rosemann
  • 505
  • 8
  • 20