Is there an idiomatic file extension for Jinja templates?

Ansible uses the .j2 extension.

I couldn't find a definitive documentation about that precise point but we see occurences of the .j2 extension in many places of their documentation :

If you look for .j2 in the following pages you'll see many occurences :

http://docs.ansible.com/ansible/template_module.html http://docs.ansible.com/ansible/playbooks_intro.html

This is the convention that I use for other projects as well, except django templates.


2021 update:: Jinja now officially recommends using the extension .jinja. check docs


Update: Things changed since I wrote this answer, .jinja2 and .j2 are trending.


Jinja Authors did not define a default extension. Most of Jinja template editors like Vim extension, TextMate extension, Emacs extension, and PyCharm mention no default extension to enforce Jinja highlighting.

Django had already a similar debate about setting a default extension, and ended as a wontfix issue. I quote from the closing message:

Filetype detection based on extension is flawed for the very reasons described in these comments, so you have to do some internal inspection, just like MIME type detection works.

I suggest that you should use your own since there is no common one.


IntelliJ's PyCharm uses .jinja2 as their pattern for recognizing Jinja2 templates. For that reason I use the same (and recommend others do so too)

pycharm filetypes