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>
运行示例 »