您好,登錄后才能下訂單哦!
在Flask中,模板繼承是通過(guò)使用Jinja2模板引擎來(lái)實(shí)現(xiàn)的。通過(guò)使用 {% extends 'base.html' %}
指令,可以讓一個(gè)模板繼承另一個(gè)模板的布局。子模板可以覆蓋父模板中的塊(block)以及添加新的塊。
下面是一個(gè)簡(jiǎn)單的示例:
base.html:
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}Flask App{% endblock %}</title>
</head>
<body>
<header>
{% block header %}
<h1>Welcome to Flask App</h1>
{% endblock %}
</header>
<main>
{% block content %}
<p>This is the content of the page.</p>
{% endblock %}
</main>
</body>
</html>
child.html:
{% extends 'base.html' %}
{% block title %}Child Template - Flask App{% endblock %}
{% block content %}
<p>This is the content of the child template.</p>
{% endblock %}
在這個(gè)例子中,child.html
模板繼承了 base.html
模板,并覆蓋了 title
和 content
塊。當(dāng)渲染 child.html
模板時(shí),會(huì)生成一個(gè)完整的HTML頁(yè)面,其中 title
為 “Child Template - Flask App”,而 content
中的內(nèi)容則是子模板中定義的內(nèi)容。
通過(guò)使用模板繼承,可以實(shí)現(xiàn)模板的復(fù)用和分離關(guān)注點(diǎn),使得頁(yè)面結(jié)構(gòu)更加清晰和易于維護(hù)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。