包含可复用的局部模板
使用 include 引入导航栏和页脚
包含可复用的局部模板 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Some Parts Are Shared, Not Page-Wide
A navbar or footer appears on many pages but is not the whole layout. For these chunks you reach for include instead of inheritance. 🧱
extends vs include
Use extends to inherit a full skeleton, but use include to drop a small reusable snippet into the current template at that spot.
The include Tag
The include tag pastes another template file right where you place it. The included file renders as if its markup were written inline.
{% include "navbar.html" %}A Partial Is Just a Template
A partial is an ordinary template holding only a fragment, like a nav bar. Naming it _navbar.html signals it is meant to be included.
<nav><a href="/">Home</a></nav>Include in Your Base
You can include a partial inside the base so every page that extends it gets the navbar automatically, with no repeated markup.
<body>
{% include "navbar.html" %}
{% block content %}{% endblock %}
</body>Partials See the Context
An included partial can read the same variables as the page that includes it, so a navbar can show the logged-in user name.
{% include "navbar.html" %}Pass Extra Context
You can hand a partial its own values with with, scoping variables so the snippet stays focused and predictable.
{% include "card.html" with context %}Tolerate a Missing File
Add ignore missing so a missing partial is skipped silently instead of crashing the page render.
{% include "ads.html" ignore missing %}Keep Partials Small
A good partial does one job: a footer, a flash message bar, a single card. Small, focused fragments are easy to reuse and reason about.
Organize Partials in a Folder
Group fragments under a partials subfolder and reference them by that path. It keeps full pages and reusable snippets clearly separated.
{% include "partials/footer.html" %}Edit Once, Update Everywhere
Fix the footer in its partial and every page that includes it updates together. That single edit is exactly why include exists. ✨
Quick Check
When should you reach for include instead of extends?
Recap: Partials
Use include to embed small shared snippets like navbars and footers. Keep them tiny, folder-organized, and edit them in one place. 🎯
常见问题解答
「包含可复用的局部模板」课时是免费的吗?
是的 — 「包含可复用的局部模板」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。
「包含可复用的局部模板」这节课中我会学到什么?
使用 include 引入导航栏和页脚 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Flask Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「包含可复用的局部模板」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Flask Academy 课中编写并运行代码吗?
能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用块定义基础模板
- 在子页面中扩展基础模板
- 包含可复用的局部模板
- 用于重复标记的宏