M S*_* SH 6 python jinja2 flask
我在文件中使用了include和并希望它们按顺序包含在内。但模板会附加到文件末尾。extendsbase.htmlextends
我希望我的模板能给我输出:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>Test String from block !</p>
<footer>text from footer.</footer>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但目前的结果是:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<footer>text from footer.</footer>
</body>
</html>
<p>Test String from block !</p>
Run Code Online (Sandbox Code Playgroud)
在base.html, 首先我包括header.html, then content.html,然后footer.html但是渲染的顺序是header.html, footer.html, content.html。
index.html
{% extends "base.html" %}
{% block content %}
<p>Test String from block !</p>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
base.html
{% include "header.html" %}
<body>
{% extends "content.html" %}
{% include "footer.html" %}
Run Code Online (Sandbox Code Playgroud)
header.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
Run Code Online (Sandbox Code Playgroud)
content.html
{% block content %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
footer.html
<footer>text from footer.</footer>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我建议结构略有不同。我最近使用了这样的结构并得到了正确的结果。
索引.html:
{% extends "base.html" %}
{% block head %}
<!-- if you want to change the <head> somehow, you can do that here -->
{% endblock head %}
{% block content %}
<body>
<p>Test String from block !</p>
{% include 'content.html' %}
</body>
{% include 'footer.html' %}
{% endblock content %}
Run Code Online (Sandbox Code Playgroud)
基本.html:
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
<meta charset="UTF-8">
<title>Title</title>
{% endblock head %}
</head>
{% block content %}
{% endblock content %}
</html>
Run Code Online (Sandbox Code Playgroud)
内容.html:
<!-- whatever you want in here -->
Run Code Online (Sandbox Code Playgroud)
页脚.html:
<footer>text from footer.</footer>
Run Code Online (Sandbox Code Playgroud)
希望这有帮助。
| 归档时间: |
|
| 查看次数: |
9954 次 |
| 最近记录: |