您可能以错误的方式考虑布局。您的布局是最通用的模板,而不是最复杂的模板。如果您需要很少的独立功能,请按原样编写它们,并include
在需要它们的地方编写它们。
也就是说,如果你想要这样的东西:
----------------------------------
+--------------+
Header | Login |
+--------------+
----------------------------------
Body Content (Blog)
而且您还希望有一个独立的登录页面,如下所示:
----------------------------------
Header
----------------------------------
+--------------+
| Login |
+--------------+
然后include
在需要的地方创建一个登录部分。
例子
模板/部分/login.html
<form action="/login" method="post">
<!-- Your login goes here -->
</form>
模板/your_base.html
<!DOCTYPE html>
<html>
<head>
{% block head %}
{#
Default HEAD content goes here
with extra nested blocks for children to override
if needed.
#}
{% endblock head %}
</head>
<body>
<header>{% block header %}{% endblock header %}</header>
{# Note: This assumes we *always* want a header #}
{% block content %}{% endblock content %}
</body>
</html>
模板/login.html
{% extends "your_base.html" -%}
{% block content -%}
{% include "partials/login.html" %}
{%- endblock content %}
模板/blog.html
{% extends "your_base.html" -%}
{% block header -%}
{{ super() }}{# Render the default header contents here #}
{% include "partials/login.html" %}
{%- endblock header %}
{% block content -%}
{# Render your blog posts here #}
{%- endblock content %}