模板继承
// base.jinja2
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" 6 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>Document</title> 9 </head> 10 <body> 11 Hello Python ! 12 <title> 13 {% block title %}1234{% endblock %} 14 </title> 15 16 {% block main %} 17 {% endblock %} 18 19 </body> 20 </html>
继承页面 (super()用于显示base.jinja2中原有的内容,这里为1234)
// index.jinja2
{% extends ‘base.jinja2‘ %} {% block title -%} {{ super() }} Name: {{ name|title }} {%- endblock %} {% block main %} {% for item in items %} {{ item }} {% endfor %} {% endblock %}
后端业务测试代码
from flask import render_template @app.route(‘/‘) def index(): return render_template(‘index.jinja2‘, name=‘bob‘, items=range(10))