Using Jinja With Python

Posted September 30, 2022 by Rohith ‐ 4 min read

In this article we will shows how to create templates in Python with Jinja module.

Python Jinja module

Jinja is a template engine for Python. It is used in django, airflow in many other frameworks.

What is a template engine?

A template engine or template processor is a library designed to combine templates with a data model to produce documents. Template engines are often used to generate large amounts of emails, in source code preprocessing, or producing dynamic HTML pages.

We create a template engine, where we define static parts and dynamic parts. The dynamic parts are later replaced with data. The rendering function later combines the templates with data.

Jinja installation

Jinja package can be installed using pip3 utility

pip3 install jinja2

Jinja delimiters

Jinja uses various delimiters in the template strings.

  • {% %} - statements
  • {{ }} - expressions to print to the template output
  • {# #} - comments which are not included in the template output
  • # ## - line statements

Jinja simple example

In the first example, we create a very simple template.


from jinja2 import Template

name = ''

template = Template("Hello {{ name }}")
message = template.render(name=name)


In the above example, name variable will be replaced with the value.

The template engine is similar to the Python format method. But template engines are more powerful and have many more features.


from jinja2 import Template

domain = ''
subdomain = 'sde'

template = Template("domain name is {{ domain }} and subdomain name is {{ subdomain }}")
message = tm.render(domain=domain, subdomain=subdomain)


The template string renders two variables: domain and subdomain

Jinja objects

We can work with objects in our template strings.

In the below example, we defined a Website object. We get the domain and subdomain via the two getters.


from jinja2 import Template

class Website:

    def __init__(self, domain, subdomain):

        self.domain = name
        self.subdomain = subdomain

    def getDomain(self):
        return self.domain

    def getSubdomain(self):
        return self.subdomain    

website = Website('', 'sde')

template = Template("domain name is {{ website.domain }} and subdomain name is {{ website.subdomain }}")
message = template.render(website=website)


Using Python Dictionaries

Jinja allows a convenient dot notation to access data in Python dictionaries.



from jinja2 import Template

website = { 'domain': '', 'subdomain': 'sde' }

template = Template("domain name is {{ website.domain }} and subdomain name is {{ website.subdomain }}")
message = template.render(website=website)


We have a website dictionary. We access the dictionary keys with a dot operator.

Jinja raw data

We can use raw, endraw markers to escape Jinja delimiters.

By using the raw, endraw block, we escape the Jinja {{ }} syntax. It is printed in its literal meaning.



from jinja2 import Template

data = '''
{% raw %}
The website name is {{ website_name }}
{% endraw %}

template = Template(data)
message = template.render(website_name='')


Jinja escape data

To escape data such as < or > characters, we can use a filter or the escape function.

Example: The below example escapes < and > characters.


from jinja2 import Template, escape

data = '<a> is awesome</a>'

template = Template("{{ data | e}}")
message = template.render(data=data)


Jinja for expressions

The for expression is used to iterate over a data collection in a template.

Now we do not use a simple string template anymore. We use a text file which is loaded with FileSystemLoader.



from jinja2 import Environment, FileSystemLoader

websites = [
    {'domain': '', 'subdomain': 'sde'}, 
    {'domain': '', 'subdomain': 'dentistry'}

file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)

template = env.get_template('show_websites.txt')

output = template.render(websites=websites)

In the template file, we use the for expression to iterate over the collection. We show the domain and the respective subdomain. The dash character next to the % characters is used to control white space.

{% for website in websites -%}
    {{ website.domain }} {{ website.subdomain }}
{% endfor %}

Jinja conditionals

Conditionals are expressions that are evaluated when a certain condition is met.


{% for website in websites %}
    {% if website.subdomain == 'sde' %}
        {{- website.subdomain }}
    {% endif %}    
{%- endfor %}
quick-references blog

Subscribe For More Content