ต่อยอดเทมเพลตพื้นฐานในหน้าลูก
ใช้ extends และเติมบล็อกแยกตามหน้า
ต่อยอดเทมเพลตพื้นฐานในหน้าลูก เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Connect a Page to the Base
A child page does not repeat the skeleton. It declares that it extends the base, then fills only the blocks it needs to change. 🔗
The extends Tag
The first line of a child template uses extends to name its parent layout. This tells Jinja2 to inherit the base before rendering.
{% extends "base.html" %}extends Must Come First
The extends tag has to be the very first thing in the child file. Putting markup above it breaks inheritance and raises a template error.
Override a Block
To replace a slot, redeclare the block with the same name in the child. Whatever you put inside now shows instead of the base default.
{% block title %}Dashboard{% endblock %}A Complete Child Page
This child template extends base.html and overrides two blocks. The rest of the page comes straight from the parent layout.
{% extends "base.html" %}
{% block title %}Home{% endblock %}
{% block content %}
<h1>Welcome</h1>
{% endblock %}Skip Blocks You Like
You only override the blocks you want to change. Any block you leave out keeps the default content defined in the base template.
Reuse Parent Content
Inside an overridden block you can call super() to pull in the parent block content, then add your own around it.
{% block content %}{{ super() }}<p>Extra</p>{% endblock %}Render the Child, Not the Base
Your view returns the child template path. Flask renders the child, which inherits the base, so you never call base.html directly.
return render_template("home.html")Block Names Must Match
Override only works when the child block name exactly matches one in the base. A typo silently leaves the parent default in place.
One Base, Many Children
Dozens of pages can extend the same base.html. Each child stays tiny because it only carries the markup unique to that page.
Inheritance Saves You
Change the base header once and every child page updates instantly. That cascade is the real power of template inheritance. 🚀
Quick Check
Where must the extends tag appear in a child template?
Recap: Extending Pages
Child pages extend a base, override matching blocks, and may reuse parent content with super(). One base now powers many pages. 🎯
คำถามที่พบบ่อย
บทเรียน “ต่อยอดเทมเพลตพื้นฐานในหน้าลูก” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ต่อยอดเทมเพลตพื้นฐานในหน้าลูก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ต่อยอดเทมเพลตพื้นฐานในหน้าลูก”
ใช้ extends และเติมบล็อกแยกตามหน้า คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ต่อยอดเทมเพลตพื้นฐานในหน้าลูก” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- กำหนดเทมเพลตพื้นฐานด้วยบล็อก
- ต่อยอดเทมเพลตพื้นฐานในหน้าลูก
- รวมส่วนประกอบที่นำกลับมาใช้ได้
- มาโครสำหรับมาร์กอัปที่ซ้ำกัน