Jinja2 is a versatile template engine that can be used for a wide range of templating and string manipulation tasks in Python applications. Here are some of the basic usages of Jinja2:
-
Variable Substitution:
Use double curly braces
{{ variable_name }}
to insert dynamic data into your templates. You can substitute variables with actual values when rendering the template.<p>Hello, {{ name }}!</p>
In Python:
template = env.get_template('template.html') context = {'name': 'John'} output = template.render(context)
-
Filters:
Filters allow you to modify variable values before they are rendered. For example, you can use the
upper
filter to convert a string to uppercase.<p>{{ text_variable | upper }}</p>
-
Control Structures:
Use
{% %}
to include control structures like loops, conditionals, and blocks in your templates. For example, you can use{% for %}
to loop through a list.<ul> {% for item in items %} <li>{{ item }}</li> {% endfor %} </ul>
-
Comments:
You can add comments to your templates using
{# #}
. Comments are not rendered and are useful for documentation or notes.{# This is a comment #}
-
Inheritance and Template Blocks:
Jinja2 supports template inheritance, allowing you to define a base template with common structure and then extend or override blocks in child templates.
Base Template (
base_template.html
):<!DOCTYPE html> <html> <head> <title>{% block title %}Default Title{% endblock %}</title> </head> <body> <div id="content"> {% block content %}{% endblock %} </div> </body> </html>
Child Template (
child_template.html
):{% extends "base_template.html" %} {% block title %}Custom Title{% endblock %} {% block content %} <p>This is custom content.</p> {% endblock %}
-
Macros:
Macros are reusable templates for commonly used components or HTML snippets. You can define macros and include them in your templates.
{% macro alert(message) %} <div class="alert"> {{ message }} </div> {% endmacro %}
Usage:
{{ alert("This is an alert!") }}
-
Template Inclusion:
You can include other templates within a template using the
{% include %}
tag.{% include "header.html" %}
This is useful for modularizing your templates.
These are some of the basic usages of Jinja2. Depending on your project, you can use Jinja2 for more advanced tasks such as internationalization, working with complex data structures, and creating custom filters and extensions.