向所有模板添加导航栏
Add a nav bar to all templates
我想在每个页面上显示一个导航栏。在 PHP 中,我会编写导航栏,然后将其包含在其他页面中。我尝试将导航栏模板包含或扩展到其他模板中,但没有成功。它只输出 "This is the home page." 如何在每个模板中正确包含导航栏?
layout.html
<!doctype html>
<html>
<body>
{% block navbar %}
<style>
body {
margin: 0;
padding: 0;
}
div{
background: #333;
color: #f9f9f9;
width: 100%;
height: 50px;
line-height: 50px;
text-align: center;
}
</style>
<div>NAVBAR</div>
{% endblock %}
{% block content %}
{% endblock %}
</body>
</html>
index.html
This is the home page.
{% extends "layout.html" %}
{% block navbar %} {% endblock %}
{% block content %}
<h1>This is the homepage!</h1>
{% endblock %}
如果您想在每个页面中使用相同的导航栏,则不需要 layout.html 中的 {% block navbar %}...{% endblock %}
。或者,您可能必须按照 here.
所述使用 {{ super() }}
使用所有页面通用的布局和导航创建基本模板。然后扩展此模板以创建实际页面。将块添加到可以在其他模板中覆盖的基本模板。
base.html
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>{% block title %} - My Site {% endblock %}</title>
</head>
<body>
<div>Navbar</div>
{% block content %}{% endblock %}
</body>
</html>
index.html
{% extends 'base.html' %}
{% block content %}
<h3>{% block title %}Home{% endblock %}</h3>
<p>Hello, World!</p>
{% endblock %}
请注意,导航栏只是在基本模板中定义的。它不需要块,子模板中的内容将在其后被替换。
您可以使用 similar technique 来控制在导航栏中突出显示的项目。
您可以在每个页面中包含导航栏。
nav.html
<style>
body {
margin: 0;
padding: 0;
}
div{
background: #333;
color: #f9f9f9;
width: 100%;
height: 50px;
line-height: 50px;
text-align: center;
}
</style>
<div>NAVBAR</div>
layout.html
: 注意 {% include 'nav.html' %}
<!doctype html>
<html>
<body>
{% include 'nav.html' %}
{% block content %}
{% endblock %}
</body>
</html>
index.html
{% extends "layout.html" %}
{% block content %}
<h1>This is the homepage!</h1>
{% endblock %}
有时,这是设计网页的好方法。你打破你的页面,例如:head.html、nav.html、footer.html...你可以将它们包含在 layout.html 中以使用它们。
我想在每个页面上显示一个导航栏。在 PHP 中,我会编写导航栏,然后将其包含在其他页面中。我尝试将导航栏模板包含或扩展到其他模板中,但没有成功。它只输出 "This is the home page." 如何在每个模板中正确包含导航栏?
layout.html
<!doctype html>
<html>
<body>
{% block navbar %}
<style>
body {
margin: 0;
padding: 0;
}
div{
background: #333;
color: #f9f9f9;
width: 100%;
height: 50px;
line-height: 50px;
text-align: center;
}
</style>
<div>NAVBAR</div>
{% endblock %}
{% block content %}
{% endblock %}
</body>
</html>
index.html
This is the home page.
{% extends "layout.html" %}
{% block navbar %} {% endblock %}
{% block content %}
<h1>This is the homepage!</h1>
{% endblock %}
如果您想在每个页面中使用相同的导航栏,则不需要 layout.html 中的 {% block navbar %}...{% endblock %}
。或者,您可能必须按照 here.
{{ super() }}
使用所有页面通用的布局和导航创建基本模板。然后扩展此模板以创建实际页面。将块添加到可以在其他模板中覆盖的基本模板。
base.html
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>{% block title %} - My Site {% endblock %}</title>
</head>
<body>
<div>Navbar</div>
{% block content %}{% endblock %}
</body>
</html>
index.html
{% extends 'base.html' %}
{% block content %}
<h3>{% block title %}Home{% endblock %}</h3>
<p>Hello, World!</p>
{% endblock %}
请注意,导航栏只是在基本模板中定义的。它不需要块,子模板中的内容将在其后被替换。
您可以使用 similar technique 来控制在导航栏中突出显示的项目。
您可以在每个页面中包含导航栏。
nav.html
<style>
body {
margin: 0;
padding: 0;
}
div{
background: #333;
color: #f9f9f9;
width: 100%;
height: 50px;
line-height: 50px;
text-align: center;
}
</style>
<div>NAVBAR</div>
layout.html
: 注意 {% include 'nav.html' %}
<!doctype html>
<html>
<body>
{% include 'nav.html' %}
{% block content %}
{% endblock %}
</body>
</html>
index.html
{% extends "layout.html" %}
{% block content %}
<h1>This is the homepage!</h1>
{% endblock %}
有时,这是设计网页的好方法。你打破你的页面,例如:head.html、nav.html、footer.html...你可以将它们包含在 layout.html 中以使用它们。