This article is part of in the series
Published: Friday 21st March 2025

jinja2 guide

Jinja2 is a designer-friendly, secure, and fast templating engine for Python. If you frequently work with web frameworks like Flask and Django, you will love it. Jinja2 lets you embed Python expressions, conditionals, and loops inside templates. This helps you build scalable and maintainable applications.

Today at PythonCentral, let us explain the basics, its syntax, and some real-world examples to help you integrate it with your Python projects. Get. Set. Learn!

How to Install Jinja

Let us take one of favourite tool Pip, to install Jinja2. Execute this command to install:

pip install Jinja2

Basics of Jinja2 Templating

Placeholders, control structures, and filters: these are the building blocks of templates. These allow dynamic content rendering.

Sample Syntax

To understand templating better, let us take up a basic Jinja2 syntax and then look at its components. Here is one:

<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>Welcome, {{ user }}!</h1>
</body>
</html>

If you look closely, you will notice:

  • {{ title }}: Placeholder for dynamic content.
  • {{ user }}: Injects a variable into the template.

How to Render Templates in Python with Jinja2

One of the most common applications of Jinja2 is rendering templates dynamically in Python. Here is a sample syntax to understand better:

from jinja2 import Template

template = Template("Hello, {{ name }}!")
message = template.render(name="PythonCentral")
print(message) # Output: Hello, PythonCentral!

How to Use Loops and Conditionals

Jinja2 supports loops and conditionals for flexible template rendering. Let us take a few examples to understand them better.

Looping Through Data

Here is a syntax that uses looping through data:

<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>

Using Conditionals

Here is a syntax that uses conditionals:

{% if user.is_admin %}
<p>Welcome, Admin!</p>
{% else %}
<p>Welcome, User!</p>
{% endif %}

Template Inheritance for Code Reusability

With Jinja 2 you can inherit from a base template, reducing code duplication. Let us take a base template and then a child template now. Here is a sample base template:

<!DOCTYPE html>
<html>
<head>
<title>{% block title %}Default Title{% endblock %}</title>
</head>
<body>
<header>{% block header %}Header Content{% endblock %}</header>
<main>{% block content %}Main Content{% endblock %}</main>
</body>
</html>

And here is the HTML code for a sample child template:

{% extends "base.html" %}

{% block title %}My Page{% endblock %}

{% block content %}
<p>This is my custom content.</p>
{% endblock %}

Using Filters for Data Formatting

Jinja2 provides filters to modify variables inside templates. Let us look at some commonly used filters now:

  • {{ name|upper }}: Converts text to uppercase
  • {{ price|round(2) }}: Rounds a number to 2 decimal places
  • {{ users|length }}: Returns the number of elements in a list

Let us try to use these filters now.

<p>Total Users: {{ users|length }}</p>
<p>Price: ${{ amount|round(2) }}</p>

Using Jinja2 in Flask Applications

Jinja2 is the default templating engine for Flask. To integrate it into a Flask app, you can spin up your own script similar to this:

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def home():
return render_template("index.html", title="Home Page", user="PythonCentral")

if __name__ == "__main__":
app.run(debug=True)

In this script,

  • render_template("index.html"): Dynamically injects data into templates.
  • Variables like "title" and "user" are passed to the template.

 Best Practices for Using Jinja2

To prevent "cross-site scripting (XSS)" and other vulnerabilities, follow these best practices:

  • Always enable autoescaping: Jinja2 escapes HTML content by default.
  • Use safe filter cautiously: Allows rendering raw HTML (we have provided an example just after this section).
  • Avoid executing user-input code: Do not use "eval()" in templates.

Here is an example of unsafe practice:

{{ user_input|safe }} {# This can be dangerous! #}

Wrapping Up

Jinja2 is an essential templating engine for Python, offering powerful features like loops, conditionals, template inheritance, and filters. Whether you are using it in Flask, Django, or standalone applications, it helps generate dynamic and scalable content efficiently. Learning Jinja2 will enhance your ability to build dynamic, efficient, and maintainable Python applications with ease. Here are some more articles you might be interested in.

Related Articles