Introduction to Jinja2 templating engine
Basics of Jinja2¶
- What is Jinja2?
- A templating engine for Python that generates dynamic HTML or other text formats.
- Integration with Flask:
- The default templating engine in Flask, allowing seamless dynamic content generation.
Key Features:
- Template Inheritance:
- Promotes code reuse by defining a base template and extending it in other templates.
- Control Structures:
- Supports conditional statements (if
, if-else
) and loops (for
).
Control Structures¶
-
If Statement:
{% if user %} <p>Welcome back, {{ user }}!</p> {% endif %}
-
If-Else Statement:
{% if user %} <p>Welcome back, {{ user }}!</p> {% else %} <p>Hello, Guest! Please log in.</p> {% endif %}
-
For Loop:
<ul> {% for item in items %} <li>{{ item }}</li> {% endfor %} </ul>
Template Inheritance¶
-
Base Template (
base.html
):<!doctype html> <html> <head> <title>{% block title %}My Website{% endblock %}</title> </head> <body> <header> <h1>My Website Header</h1> </header> <main> {% block content %}{% endblock %} </main> <footer> <p>My Website Footer</p> </footer> </body> </html>
-
Child Template (
index.html
):{% extends "base.html" %} {% block title %}Home - My Website{% endblock %} {% block content %} <h2>Welcome to the Home Page</h2> <p>This is the content of the home page.</p> {% endblock %}
Benefits of Jinja2¶
- Separation of Concerns:
- Separates application logic from presentation logic.
- Reusability:
- Reuse common layout elements, enhancing consistency and maintainability.
Jinja2 makes it easy to generate dynamic, reusable, and maintainable HTML by providing powerful features like template inheritance and control structures.