Jinja2 Template: The modern design friendly templating engine


Jinja2 is a modern and design friendly templating engine for Python programming language. Jinja2 is a library for Python that is flexible, fast and secure.Jinja can generate any text-based format (HTML, XML, CSV, JSON, etc.). It can generate different documents based on one or more predefined templates. It is also used as a part of the backend of the websites where we need to create many similar pages.

jinja

Features

  • Compiles down to the optimal python code just in time
  • Easy to debug
  • Configurable syntax
  • Sandboxed execution
  • Powerful automatic HTML escaping system

Installation


Jinja2 works with Python 2.6.x, 2.7.x and >= 3.3. If you are using Python 3.2 you can use an older release of Jinja2 (2.6) as support for Python 3.2 was dropped in Jinja2 version 2.7.

Python can be installed from here

Jinja2 can be installed easily using pip:
pip install jinja2

Some Basics of Jinja2

A Jinja template is a simple text file. Jinja can generate any text-based format. A Jinja template doesn’t need to have a specific extension such as .html, .xml, or any other extension is just fine. A template contains variables and/or expressions, which get replaced with values when a template is rendered; and tags, which control the logic of the template.

There are a few kinds of delimiters. The default Jinja delimiters are configured as follows:

  • {% … %} for Statements
  • {{ … }} for Expressions to print to the template output
  • {# … #} for Comments not included in the template output
  • # … # for Line Statements

There are two key objects in Jinja, called Environment and Template. Environment objects are used to initialize, store, and configure variables which are required for template rendering. Template objects represent a template file and can be used to generate one or more outputs from it.

There is also a secondary set used for reading the template files. These objects are known as Loaders such as FileSystemLoader, PackageLoader, and DictLoader. These objects read template data from the file system, a Python package, or a Python dictionary, respectively.

The Process of generating a document from a template and its inputs has four steps:

  1. Select and construct an appropriate loader object as:

loaderObj = jinja2.FileSystemLoader('./')

Loaders can be different for different template sources.

2. Create an environment object while specifying that new loader and any other desired options as:

jinjaEnv = jinja2.Environment(loader=loaderObj)

3. Use the environment’s get_template method to read the template file using the loader as:

jTemplate = jinjaEnv.get_template(TEMPLATE_FILE)

4. Process the template by passing any inputs into the render method of the template object as:

output = jTemplate.render(Input_for_template)

Implementing Jinja2

First, we will try to render an HTML template using Jinja2 template engine. So we will create 3 files for this: a template file, a file which contains inputs for the template and finally a python script file for processing it.

Our HTML template will look like this. Here we have created 3 configurable variables which will be replaced after rendering this template. Create a template file as “html-template.j2” with the content given below:

Screenshot from 2018-02-06 14-44-26

Now we create a YAML file for giving some inputs to the template file. So create a YAML file as “input.yml” with the content given below:

title: Jinja2
heading_1: Jinja2 Welcomes You
paragraph_1: Jinja2 is a modern and designer-friendly templating language for Python. It is fast,
widely used and secure with the optional sandboxed template execution environment.

And Finally create a small python script file to complete this process as:

#!/usr/bin/python
import jinja2, yaml
templateFilePath = jinja2.FileSystemLoader('./')
jinjaEnv = jinja2.Environment(loader=templateFilePath)
jTemplate = jinjaEnv.get_template("html-template.j2")
with open("input.yml") as config:
config = yaml.load(config)
output = jTemplate.render(config)
print output
outFileH = open('index.html', 'w')
outFileH.write(output)
outFileH.close()

This python script will produce an HTML file called ‘index.html’, which is generated from the template and input variables from a yaml file.

Thanks.

References

 

This entry was posted in Devops, Scala and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s