include语句
include
语句可以把一个模板引入到另外一个模板中,类似于把一个模板的代码copy到另外一个模板的指定位置
格式:
{% include 网页名称.html’ %}
应用
- 创建
header.html
<h3>网页头</h3>
- 创建
footer.html
<h3>网页尾部</h3>
- 创建测试文件
test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% include 'header.html' %} <p>网页内容</p> {% include 'footer.html'%} </body> </html>
- python文件
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route("/test") def test(): return render_template('test.html') if __name__ == '__main__': app.run(debug=True)
- 结果: