jinja
ProgrammingTech/Web

What is Jinja?

Introduction

Jinja is a powerful and popular templating engine for Python. It is widely used in web frameworks like Flask and Django to generate dynamic HTML pages. Jinja allows developers to use control structures, filters, and macros to manipulate template data efficiently.

Features of Jinja

  • Template Inheritance: Allows reuse of templates by extending base templates.
  • Control Structures: Includes loops (for), conditionals (if/else), and more.
  • Filters: Built-in functions to modify variables inside templates.
  • Macros: Functions that allow reusable template code.
  • Escaping: Prevents XSS attacks by escaping dangerous characters.
  • Integration: Works seamlessly with Python frameworks like Flask and Django.

Installation of Jinja

Jinja is included with Flask but can also be installed separately using pip:

pip install Jinja2

To verify the installation, check the version:

python -c "import jinja2; print(jinja2.__version__)"

Basic Syntax of Jinja

Jinja templates use curly braces ({{ }}) for expressions and {% %} for statements.

Example Template

<!DOCTYPE html>
<html>
<head>
    <title>{{ title }}</title>
</head>
<body>
    <h1>Welcome, {{ user }}</h1>
    {% if user == "Admin" %}
        <p>You have admin privileges.</p>
    {% else %}
        <p>Standard user access.</p>
    {% endif %}
</body>
</html>

Rendering the Template in Python

from jinja2 import Template

template = Template("Hello, {{ name }}!")
message = template.render(name="Harshvardhan")
print(message)

Output:

Hello, Harshvardhan!

Control Structures in Jinja

If-Else Condition

{% if age >= 18 %}
    <p>You are eligible to vote.</p>
{% else %}
    <p>You are not eligible to vote.</p>
{% endif %}

Loops (For Loop)

<ul>
{% for item in items %}
    <li>{{ item }}</li>
{% endfor %}
</ul>

Using Filters

Filters modify variables in templates.

<p>{{ name | upper }}</p>  <!-- Converts text to uppercase -->
<p>{{ price | round(2) }}</p>  <!-- Rounds a number to 2 decimal places -->

Template Inheritance

Jinja supports template inheritance using {% extends %} and {% block %}.

Base Template (base.html)

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}Default Title{% endblock %}</title>
</head>
<body>
    <header>{% block header %}Default Header{% endblock %}</header>
    <main>{% block content %}Default Content{% endblock %}</main>
</body>
</html>

Child Template (child.html)

{% extends "base.html" %}

{% block title %}Home Page{% endblock %}
{% block header %}Welcome to My Website{% endblock %}
{% block content %}<p>This is the home page content.</p>{% endblock %}

Using Jinja with Flask

Jinja is the default templating engine in Flask. To render an HTML template:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html', title='Home Page', user='Harshvardhan')

if __name__ == '__main__':
    app.run(debug=True)

Conclusion

Jinja is a versatile and efficient templating engine that simplifies dynamic content rendering in Python applications. Its integration with web frameworks like Flask makes it an essential tool for developers. Mastering Jinja can significantly enhance the efficiency of your web development projects.

For more details, check the official Jinja documentation: https://jinja.palletsprojects.com.

You may like also: Best Portable Operating Systems

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

3 thoughts on “What is Jinja?

Leave a Reply

Your email address will not be published. Required fields are marked *