jinja
A very fast and expressive template engine.
Jinja is a Python templating engine that fills placeholders in HTML or text files with real data using loop and conditional logic, with automatic HTML escaping to prevent security vulnerabilities in web apps.
Jinja is a templating engine for Python. In plain terms, a templating engine takes a text file with placeholders and fills in real values to produce a finished document, such as an HTML web page. You write the template once, then pass different data to it as many times as needed.
The syntax inside a Jinja template looks similar to Python code. You can write loops, conditionals, and labeled blocks directly in the file. One of its most useful features is template inheritance: you create a base page layout and extend it in other files, overriding only the sections that change. This keeps web projects from repeating the same header and footer code in every file.
Jinja includes safety features that matter when building web applications. HTML templates can automatically escape special characters, which prevents a common security problem called XSS (cross-site scripting), where malicious input from a user could be injected into a page. There is also a sandboxed mode for safely processing templates that come from untrusted sources.
Under the hood, Jinja compiles templates into Python code the first time they run and caches the result, which is why the project describes itself as fast. It also supports Python's async features, internationalization via Babel, and can be extended with custom filters and functions.
Jinja is maintained by the Pallets organization, the same group that maintains the Flask web framework. It is widely used as Flask's default templating language and is one of the most common ways to generate HTML in Python web projects. The README is short and points to external documentation for fuller detail.
Where it fits
- Generate HTML pages in a Flask or Python web app by passing data variables to Jinja templates instead of building HTML strings in code.
- Create a base page layout template and extend it across all pages, overriding only the content block each time to avoid duplicating header and footer code.
- Render configuration files, emails, or markdown documents from templates with variable substitution, loops, and conditionals.
- Use sandboxed Jinja mode to safely render templates submitted by untrusted users without allowing code execution.