Fork me on GitHub

Jinja2

Jinja2 is a Python template engine used to create HTML, XML or other markup formats that are returned to the user via an HTTP response.

Why is Jinja2 useful?

Jinja2 is useful because it has consistent template tag syntax and the project is cleanly extracted as an independent open source project so it can be used as a dependency by other code libraries.

Jinja2 is an implementation of the template engines concept. Learn more in the web development chapter or view the table of contents for all topics.

Jinja2 strikes a thoughtful balance on the template engine spectrum where on one end you can embed arbitrary code in the templates and the other end a developer can code whatever she wants.

Jinja2 origin and development

The first recorded public released of Jinja2 was in 2008 with 2.0rc1. Since then the engine has seen numerous updates and remains in active development.

Jinja2 engine certainly wasn't the first template engine. In fact, Jinja2's syntax is inspired by Django's built-in template engine, which was released several years earlier. There were many template systems, such as JavaServer Pages (JSPs), that originated almost a decade before Jinja2. Jinja2 built upon the concepts of other template engines and today is widely used by the Python community.

What projects depend on Jinja2?

Jinja2 is a commonly-used templating engine for web frameworks such as Flask, Bottle, Morepath and, as of its 1.8 update, optionally Django as well. Jinja2 is also used as a template language by configuration management tool Ansible and the static site generator Pelican, among many other similar tools.

The idea is that if a developer already knows Jinja2 from working with one project then the exact same syntax and style can be used in another project that requires templating. The re-use reduces the learning curve and saves the open source project author from having to reinvent a new templating style.

Jinja2 resources

Learn more about template engines or move to a new topic?

What are template engines and why are they useful?

I want to learn how to code a Python web application using a framework.

I've built a Python web app, now how do I deploy it?

Sign up here to receive a monthly email with major updates to this site, tutorials and discount codes for Python books.