What are Templates?
Back in the days, servers used to have a collection of files, like HTML files, which were sent over as requested by clients. These were static data being sent over.
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.
What is Jinja2?
Jinja2 is a modern and designer-friendly templating language for Python frameworks. It is fast, reliable and widely used for dynamic file generation based on its parameter.
It is used to create HTML, XML or other markup formats that are returned to the user via an HTTP request.
Jinja2 Features:
- Sandboxed execution
- Powerful automatic HTML escaping system for XSS prevention
- Template inheritance
- Compiles down to the optimal python code just in time
- Optional ahead-of-time template compilation
- Easy to debug. Line numbers of exceptions directly point to the correct line in the template.
- Configurable syntax
Why do we need Jinja 2?
Sandboxed Execution: It provides a protected framework for automation of testing programs, whose behaviour is unknown and must be investigated.
HTML Escaping: Jinja 2 has a powerful automatic HTML Escaping, which helps preventing 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 is the most important feature, which I will expand on to later in the post.
Delimiters Used in Jinja 2
{%....%} are for statements such as condition and loop
{{....}} are expressions used to print to template output
{#....#} are for comments which are not included in the template output
#....## are used as line statements
How To Install Jinja 2
$ pip install jinja2
$ easy_install jinja2
Refernece
- https://jinja.palletsprojects.com/en/2.11.x/
- https://codeburst.io/jinja-2-explained-in-5-minutes-88548486834e
- https://mydbops.wordpress.com/2019/04/17/jinja2-for-better-ansible/
- Discover the Heart of India with Mera Apna Bihar - December 20, 2024
- Experience the Pulse of Bangalore with Bangalore Orbit - December 20, 2024
- Discover the City of Lakes with Bhopal Orbit - December 20, 2024