Jinja is a template engine (it is like a language on its own) that helps to place strings in documents. Essentially, you have a document with placeholders or variables called template. The data to be placed is provided by another file or from user.
Jinja takes the data from the user and places it at the placeholders in the template to give you the desired document.
This is more powerful than the built-in string formatter of Python.
It is used with Flask and Django web frameworks for making web pages.
However, it is generic in nature and can be used in many ways.
These notes are taken from a YouTube Tutorial on Jinja2 by Jason Rigden
Let’s say that the template file had a sentence like this:
{{name}} had a little {{animal}}
Here, name
is a variable and it is placed within backticks. Same with animal
. The curly brackets are placeholders for the variables.
Suppose name = 'Mary'
and animal = 'Lamb'
are the values provided by the user.
Then Jinja will take these values and replace the variables in the template to give this file:
Mary had a little Lamb
The following examples go from a simplistic case to an advanced case step-by-step.
Note: This is a preliminary “Hello, World!” program to sort out the basics!
from jinja2 import Environment, FileSystemLoader
= FileSystemLoader('templates') file_loader
Note that the directory/folder name here can by anything!
Create an enviroment variable and pass the file loader variable to it.
= Environment(loader=file_loader) env
Now, the environment is ready.
Now, create an input text file and write contents into it. We’ll call it “hello_world.txt”
Hello, world!
= env.get_template('hello_world.txt') template
= template.render()
output print(output)
You can pass variables to the render()
method. But, that is for a future example.
Run the python file and the output will be:
Hello, World!
This is the full code:
# Import the required methods
from jinja2 import Environment, FileSystemLoader
# Specify the template directory and environment
= FileSystemLoader('templates')
file_loader = Environment(loader=file_loader)
env
# Create a template variable via environment
= env.get_template('hello_world.txt')
template
# Render the template and create an output variable.
= template.render()
output
# Print it out to see or export it to a new file as you see fit.
print(output)
# Import the required methods
from jinja2 import Environment, FileSystemLoader
# Specify the template directory and environment
= FileSystemLoader('templates')
file_loader = Environment(loader=file_loader) env
lamb.txt
{{name}} had a little {{animal}}.
# Create a template variable via environment
= env.get_template('lamb.txt')
template
# Render the template and create an output variable.
= template.render(name='Mary', animal='Lamb')
output
# Print it out to see or export it to a new file as you see fit.
print(output)
Mary had a little Lamb.
# Import the required methods
from jinja2 import Environment, FileSystemLoader
# Specify the template directory and environment
= FileSystemLoader('templates')
file_loader = Environment(loader=file_loader) env
lamb.txt
{{data.name}} had a little {{data.animal}}.
# Create a template variable via environment
= env.get_template('lamb.txt')
template
# Create a dictionary
= {}
person 'name'] = 'Mary'
person['animal'] = 'Lamb'
person[
# Render the template and create an output variable.
= template.render(data=person)
output
# Print it out to see or export it to a new file as you see fit.
print(output)
Mary had a little Lamb.
# Import the required methods
from jinja2 import Environment, FileSystemLoader
# Specify the template directory and environment
= FileSystemLoader('templates')
file_loader = Environment(loader=file_loader) env
truth.txt
% if truth %}
{is true
This % else %}
{is false
This % endif %} {
= env.get_template('truth.txt')
template = template.render(truth=True)
output print(output)
This is true
= env.get_template('truth.txt')
template = template.render(truth=False)
output print(output)
This is false
# Import the required methods
from jinja2 import Environment, FileSystemLoader
# Specify the template directory and environment
= FileSystemLoader('templates')
file_loader = Environment(loader=file_loader) env
rainbow.txt
% for colour in colours %}
{
{{colour}}% endfor %} {
= env.get_template('rainbow.txt')
template
= ['red', 'green', 'blue']
colours = template.render(colours=colours)
output print(output)
red
green
blue
# Import the required methods
from jinja2 import Environment, FileSystemLoader
# Specify the template directory and environment
= FileSystemLoader('templates')
file_loader = Environment(loader=file_loader) env
header.html
- Has the title. It will be included in the template.<head>
<title>{{title}}</title>
</head>
base.html
- This is the base file.<html>
{% include 'header.html' %}<body>
</body>
</html>
= env.get_template('base.html')
template
= template.render(title="Hey, there!")
output print(output)
<html>
<head>
<title>Hey, there!</title>
</head>
<body>
</body>
</html>
Observation: Here, the base.html
file automatically called header.html
file. And inside it, the title variable was automatically placed.
# Import the required methods
from jinja2 import Environment, FileSystemLoader
# Specify the template directory and environment
= FileSystemLoader('templates')
file_loader = Environment(loader=file_loader) env
header.html
- Has the title. It will be included in the template.<head>
<title>{{title}}</title>
</head>
base.html
- This is the base file with content!<html>
{% include 'header.html' %}<body>
{% block content %}
{% endblock %}</body>
</html>
child.html
- Contents of this file fill be placed.
{% extends "base.html" %}
{% block content %}
<p>
{{body}}</p>
{% endblock %}
= env.get_template('child.html')
template
= template.render(title="Hey, there!", body="Brown cow!")
output print(output)
<html>
<head>
<title>Hey, there!</title>
</head>
<body>
<p>
Brown cow!</p>
</body>
</html>