As a memorandum.
templates/index.html
{% extends 'base.html' %}
{% block main %}
<main>
<p>Main content</p>
<p><a href="/another">Go to another page</a></p>
</main>
{% endblock %}
templates/another.html
{% extends 'base.html' %}
{% block main %}
<main>
<p>Another content</p>
<p><a href="/">Back to top page</a></p>
</main>
{% endblock %}
templates/base.html
<html>
<head>
<title>Flask lesson</title>
</head>
<body>
<header>
<div><h1>HEADER</h1></div>
<hr>
</header>
{% block main %}
{% endblock %}
<footer>
<hr>
<div><h1>FOOTER</h1></div>
</footer>
</body>
</html>
run.py
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/another')
def second():
return render_template('another.html')
app.debug = True
app.run()
--extend means to extend --Specify the extension destination html with {% extends ~~~ .html%} -Enclose the code you want to embed in {% block ~~~%} {% endblock%}, and write it in the location you want to embed in the extension destination --You can also write and embed multiple {% block ~~~%} {% endblock%}
PHP include may be easier to manage.
Recommended Posts