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.

$ sudo pip3 install jinja2

We use the pip3 tool to install Jinja.

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

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


#!/usr/bin/env python3from jinja2 import Templatename = input("Enter your name: ")tm = Template("Hello  name ")
msg = tm.render(name=name)print(msg)

The example asks for a user name and generates a message string, which is printed to the user. 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

We import the Template object from the jinja2 module. Template is the central template object. It represents a compiled template and is used to evaluate it.

tm = Template("Hello  name ")

In our template, we have the syntax which is used to print the variable. The variable is passed in the render() method.

msg = tm.render(name=name)

With the render() method, we generate the final output. The method joins the template string with the data passed as argument. The variable that is passed to the render() method is called the context variable.

$ ./simple.py
Enter your name: Paul
Hello Paul

This is a sample output.

In the next example, we use two variables.


#!/usr/bin/env python3from jinja2 import Templatename = 'Peter'
age = 34tm = Template("My name is  name  and I am  age ")
msg = tm.render(name=name, age=age)print(msg)

The template string renders two variables: name and age. This time the variables are hard-coded.

$ ./simple2.py
My name is Peter and I am 34

This is the output.

We can work with objects in our template strings.


#!/usr/bin/env python3from jinja2 import Templateclass Person:    def __init__(self, name, age):        self.name = name
self.age = age    def getAge(self):
return self.age    def getName(self):
return self.name
person = Person('Peter', 34)tm = Template("My name is  per.getName()  and I am  per.getAge() ")
msg = tm.render(per=person)print(msg)

In the example, we define a Person object. We get the name and age via the two getters.

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


#!/usr/bin/env python3from jinja2 import Templateperson =  'name': 'Person', 'age': 34 tm = Template("My name is  per.name  and I am  per.age ")
# tm = Template("My name is  per['name']  and I am  per['age'] ")
msg = tm.render(per=person)print(msg)

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

tm = Template("My name is  per.name  and I am  per.age ")
# tm = Template("My name is  per['name']  and I am  per['age'] ")

Both the active and the commented way are valid. The dot notation is more convenient.

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


#!/usr/bin/env python3from jinja2 import Templatedata = '''
% raw %
His name is  name
% endraw %
'''tm = Template(data)
msg = tm.render(name='Peter')print(msg)

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

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


#!/usr/bin/env python3from jinja2 import Template, escapedata = 'Today is a sunny day'tm = Template(" e")
msg = tm.render(data=data)print(msg)

The example escapes < and > characters.

tm = Template(" data ")

Using the e filter, the data is escaped. Filters are applied with the | character.


The escape function does the same.

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.


#!/usr/bin/env python3from jinja2 import Environment, FileSystemLoaderpersons = [
'name': 'Andrej', 'age': 34,
'name': 'Mark', 'age': 17,
'name': 'Thomas', 'age': 44,
'name': 'Lucy', 'age': 14,
'name': 'Robert', 'age': 23,
'name': 'Dragomir', 'age': 54
]file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)template = env.get_template('showpersons.txt')output = template.render(persons=persons)

In this example, the template is the showpersons.txt file. The file is located in the templates directory.

persons = [
'name': 'Andrej', 'age': 34,
'name': 'Mark', 'age': 17,
'name': 'Thomas', 'age': 44,
'name': 'Lucy', 'age': 14,
'name': 'Robert', 'age': 23,
'name': 'Dragomir', 'age': 54

The data is a list of dictionaries.

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

We define a FileSystemLoader. The template is retrieved from the templates directory.

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

We get the template with the get_template() method.


% for person in persons -%
 person.name   person.age
% endfor %

In the template file, we use the for expression to iterate over the collection. We show the person’s name and age. The dash character next to the % characters is used to control white space.

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


#!/usr/bin/env python3from jinja2 import Environment, FileSystemLoaderpersons = [
'name': 'Andrej', 'age': 34,
'name': 'Mark', 'age': 17,
'name': 'Thomas', 'age': 44,
'name': 'Lucy', 'age': 14,
'name': 'Robert', 'age': 23,
'name': 'Dragomir', 'age': 54,
]file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)
env.trim_blocks = True
env.lstrip_blocks = True
env.rstrip_blocks = Truetemplate = env.get_template('showminors.txt')output = template.render(persons=persons)

The example prints only minor persons; a minor is someone younger than 18.

env.trim_blocks = True
env.lstrip_blocks = True
env.rstrip_blocks = True

White space in output can be controlled with environment attributes.


% for person in persons %
% if person.age < 18 %
- person.name
% endif %
%- endfor %

In the template, we output only persons younger than 18 using if expression.

$ ./conditionals.py

This is the output.

Filters can be applied to data to modify them. For instance, the sum filter can sum data, escape filter escapes them, and sort filter sorts them.


#!/usr/bin/env python3from jinja2 import Environment, FileSystemLoadercars = [
'name': 'Audi', 'price': 23000,
'name': 'Skoda', 'price': 17300,
'name': 'Volvo', 'price': 44300,
'name': 'Volkswagen', 'price': 21300
]file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)template = env.get_template('sumprices.txt')output = template.render(cars=cars)

In the example, we use the sum filter to calculate the sum of all car prices.

cars = [
'name': 'Audi', 'price': 23000,
'name': 'Skoda', 'price': 17300,
'name': 'Volvo', 'price': 44300,
'name': 'Volkswagen', 'price': 21300

We have a list of car dictionaries. Each dictionary has a price key. It will be used to calculate the sum.


The sum of car prices is  cars 

In the template file, we apply the filter on the cars collection object. The sum is calculated from the price attribute.

$ ./sum_filter.py
The sum of car prices is 105900

This is the output.

Template inheritance is a powerful feature that reduces code duplication and improves code organization. We define a base template from which we inherit in other template files. These template files overwrite specific blocks of the base template file.

#!/usr/bin/env python3from jinja2 import Environment, FileSystemLoadercontent = 'This is about page'file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)template = env.get_template('about.html')output = template.render(content=content)

We render the about.html file. It inherits from the base.html file.


% block title %% endblock %
% block content%
% endblock %

In the base.html file, we declare two blocks: title and content. These blocks are going to be filled with specific tags and text in the child templates.


% extends 'base.html' %% block title%About page% endblock %% block content %

About page

This is about page % endblock %

The about.html template file inherits from base.html. It adds data specific to this page. We avoid code repetition; we do not repeat tags that are same for both pages, such as body and html and meta tags.The inheritance is done with the extends directive.

Source link

Leave a Reply