我在include
文件中使用了extends
和base.html
,并期望它们按顺序包含在内。但是extends
模板会附加到文件的末尾。
我希望我的模板能够输出:
<!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>
但目前的结果是:
<!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>
在base.html
中,首先我添加header.html
,然后content.html
,然后footer.html
,但呈现的顺序为header.html
,footer.html
,{ {1}}。
content.html
index.html
{% extends "base.html" %}
{% block content %}
<p>Test String from block !</p>
{% endblock %}
base.html
{% include "header.html" %}
<body>
{% extends "content.html" %}
{% include "footer.html" %}
header.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
content.html
{% block content %}
{% endblock %}
footer.html
答案 0 :(得分:2)
我建议稍微改变结构。我刚刚使用了这样的结构并得到了正确的结果。
的index.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 %}
base.html文件:
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
<meta charset="UTF-8">
<title>Title</title>
{% endblock head %}
</head>
{% block content %}
{% endblock content %}
</html>
content.html:
<!-- whatever you want in here -->
footer.html:
<footer>text from footer.</footer>
希望这有帮助。
答案 1 :(得分:0)
我认为您希望在
中使用include
代替extends
{% extends "content.html" %}