What IS Jinja

Short introduction to Jinja

Jinja is a modern and designer-friendly templating language for Python, modeled after Django’s templates. It is fast, widely used, and secure with the optional sandboxed template execution environment. Jinja is basically an engine used to generate HTML or XML returned to the user via an HTTP response.

For those who have not been exposed to a templating language before, such languages essentially contain variables as well as some programming logic, which when evaluated (or rendered into HTML) are replaced with actual values.

Why do we need Jinja?

Sandboxed Execution - It provides a protected framework for automation of testing programs, whose behavior is unknown and must be investigated.

HTML Escaping - Jinja has a powerful automatic HTML Escaping, which helps to prevent Cross-site Scripting (XSS Attack). There are special characters like >,<,&, etc. which carry special meanings in the templates. So, if you want to use them as regular text in your documents then, replace them with entities. Not doing so might lead to XSS-Attack.

Template Inheritance - This feature helps us to generate new pages starting from a base template that we inherit a common structure.

Jinja Environment

Being a Python library, Jinja requires Python to run and expose the features. If you're not sure if Python is installed, just open a terminal and type python --version. The output should be something like this:

$ # Check Python version
$ python --version
Python 3.7.2 # <--- All good

How to get Jinja2

To start playing with it, just open a terminal and type:

$ pip install jinja2

Jinja in action

Simple runtime replace

>>> from jinja2 import Template
>>> t = Template("Hello {{ token }}!")
>>> t.render(token="Jinja2")
u'Hello Jinja2!'

The engine will replace the inner token with value Jinja2. This is quite useful when we use this block for different token values.

Lists iteration

In web development, we can have cases when a list should be displayed on the page: registered users, for instance, or a simple list of options. In Jinja, we can use a for structure as bellow:

# Define data structure
my_list=[0,1,2,3,4,5] # a simple list with integers

In Jinja, we can iterate with ease, using a for block:

...
      <ul>
        
{% for n in my_list %}
        <li>{{n}}</li>
        {% endfor %}

      </ul>
...

Template Inheritance

Templates usually take advantage of inheritance, which includes a single base template that defines the basic structure of all subsequent child templates. You use the tags { extends } and { block } to implement inheritance.

Let's take a look at a real sample:

Parent HTML - saved as base.html

<html>
  <head>
    <title>My Jinja 
{% block title %}{% endblock %} </title>
  </head>
  <body>
    <div class="container">
      <h2>This is from the base template</h2>
      <br>
      { block content }{ endblock }
      <br>
    </div>
  </body>
</html>

The Child template - saved as child.html

{ extends "base.html" }

{ block title } MySample { endblock }

{ block content }
  Cool content here
{ endblock }

When Jinja loads child.html, the { extends } block informs the engine to merge the base.html template with the content provided by child.html.

  • { block title } becomes MySample

  • { block content } becomes Cool content here

Here is the final HTML generated by Jinja:

<html>
  <head>
    <title>My Jinja MySample</title>
  </head>
  <body>
    <div class="container">
      <h2>This is from the base template</h2>
      <br>
      Cool content here
      <br>
    </div>
  </body>
</html>

Last updated