Home > Article > Backend Development > A deep dive into Django’s template engine and Flask’s Jinja2
In-depth understanding of Django's template engine and Flask's Jinja2 requires specific code examples
Introduction:
Django and Flask are two commonly used and popular ones in Python Web framework. They both provide powerful template engines to handle the rendering of dynamic web pages. Django uses its own template engine, while Flask uses Jinja2. This article will take an in-depth look at Django’s template engine and Flask’s Jinja2, and provide some concrete code examples to illustrate their usage and differences.
1. Django template engine
<!DOCTYPE html> <html> <head> <title>Hello</title> </head> <body> <h1>Hello, {{ name }}!</h1> </body> </html>
In the view function, we can use the render
function to The data is passed to the template and rendered. For example, we can pass a variable named "name" to the template:
from django.shortcuts import render def hello(request): name = "John" return render(request, 'hello.html', {'name': name})
{{ name }}
. {% for item in items %} {% endfor %}
. {{ name|title }}
means converting the name variable to a capitalized form. <!DOCTYPE html> <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> <header> {% block header %}{% endblock %} </header> <div class="content"> {% block content %}{% endblock %} </div> <footer> {% block footer %}{% endblock %} </footer> </body> </html>
Then, in other templates we can use the extends
key Words to inherit and extend the basic template, as shown below:
{% extends 'base.html' %} {% block title %}Hello{% endblock %} {% block header %} <h1>Welcome to my website!</h1> {% endblock %} {% block content %} <p>Hello, {{ name }}!</p> {% endblock %}
2. Creation and use of Flask’s Jinja2
<!DOCTYPE html> <html> <head> <title>Hello</title> </head> <body> <h1>Hello, {{ name }}!</h1> </body> </html>
In the view function, you can use the render_template
function to render Template and pass data. For example, we can pass a variable named "name" to the template:
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def hello(): name = "John" return render_template('hello.html', name=name)
{{ name }}
. {% for item in items %} {% endfor %}
. {{ name|title }}
means converting the name variable to capitalized form. . <!DOCTYPE html> <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> <header> {% block header %}{% endblock %} </header> <div class="content"> {% block content %}{% endblock %} </div> <footer> {% block footer %}{% endblock %} </footer> </body> </html>
Then, use the extends
keyword in other templates To inherit and extend the basic template, as shown below:
{% extends 'base.html' %} {% block title %}Hello{% endblock %} {% block header %} <h1>Welcome to my website!</h1> {% endblock %} {% block content %} <p>Hello, {{ name }}!</p> {% endblock %}
Conclusion:
Both Django’s template engine and Flask’s Jinja2 provide powerful functions to create dynamic web page renderings. There are some slight differences in syntax and usage between the two, but overall they are very similar. In actual development, you can choose to use one of them based on personal preferences or project needs.
The above is an in-depth understanding of Django's template engine and Flask's Jinja2. Through specific code examples, we have a clearer understanding of their usage and differences. I hope this article provides some help for you to understand and use these two template engines.
The above is the detailed content of A deep dive into Django’s template engine and Flask’s Jinja2. For more information, please follow other related articles on the PHP Chinese website!