Jinja2 is a modern and designer-friendly templating language for Python, modelled after Django’s templates. Jinja2 is a modern day templating language for Python developers. It was made after Django’s template. It is used to create HTML, XML or other markup formats that are returned to the user via an HTTP request. If you have any exposure to other text-based template languages, such as Smarty or Django, you should feel right at home with Jinja2. It’s both designer and developer friendly by sticking to Python’s principles and adding functionality useful for templating environments.
The Jinja template engine allows customization of tags, filters, tests, and globals. Also, unlike the Django template engine, Jinja allows the template designer to call functions with arguments on objects. Jinja is Flask’s default template engine.
- Sandboxed execution mode. Every aspect of the template execution is monitored and explicitly whitelisted or blacklisted, whatever is preferred. This makes it possible to execute untrusted templates.
- powerful automatic HTML escaping system for cross site scripting prevention.
- Template inheritance makes it possible to use the same or a similar layout for all templates.
- High performance with just in time compilation to Python bytecode. Jinja2 will translate your template sources on first load into Python bytecode for best runtime performance.
- Optional ahead-of-time compilation
- Easy to debug with a debug system that integrates template compile and runtime errors into the standard Python traceback system.
- Template designer helpers. Jinja2 ships with a wide range of useful little helpers that help solving common tasks in templates such as breaking up sequences of items into multiple columns and more.
How To Use Jinja2
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.
You have multiple ways to install Jinja2. If you are unsure what to do, go with the Python egg or tarball.
This will install a Jinja2 egg in your Python installation’s site-packages directory.
From the tarball release
- Download the most recent tarball from the download page
- Unpack the tarball
python setup.py install
Note that you either have to have setuptools or distribute installed; the latter is preferred.
This will install Jinja2 into your Python installation’s site-packages directory.
Installing the development version
- Install git
git clone git://github.com/pallets/jinja.git
ln -s jinja2 /usr/lib/python2.X/site-packages
As an alternative to steps 4 you can also do
python setup.py develop which will install the package via distribute in development mode. This also has the advantage that the C extensions are compiled.
As of version 2.7 Jinja2 depends on the MarkupSafe module. If you install Jinja2 via pip or easy_install it will be installed automatically for you.
What are Templates?
A template contains variables which are replaced by the values which are passed in when the template is rendered. Variables are helpful with the dynamic data.
Now, in the modern web world, we have less of static data and more of dynamic data being requested from clients and therefore sent by the server. The web totally depends on what the client is asking for, and on which user is signing in and who is logging out. So, Jinja2 templating is being used.