Django include 標籤
Include
include
標籤允許你在當前模板中包含另一個模板。
這在有許多頁面都相同的程式碼塊時非常有用。
示例
templates/footer.html
:
<p>You have reached the bottom of this page, thank you for your time.</p>
templates/template.html
:
<h1>Hello</h1>
<p>This page contains a footer in a template.</p>
{% include 'footer.html' %}
執行示例 »
Include 中的變數
你可以透過使用 with
關鍵字將變數傳遞到模板中。
在 include 檔案中,你可以使用 {{
變數名 }}
語法來引用變數。
示例
templates/mymenu.html
:
<div>HOME | {{ me }} | ABOUT | FORUM | {{ sponsor }}</div>
templates/template.html
:
<!DOCTYPE html>
<html>
<body>
{% include "mymenu.html" with me="TOBIAS" sponsor="W3SCHOOLS" %}
<h1>Welcome</h1>
<p>This is my webpage</p>
</body>
</html>
執行示例 »