bao gồm Thẻ mẫu
Ví dụ
Bao gồm một mẫu bên trong mẫu:
<!DOCTYPE html>
<html>
<body>
{% include mymenu.html %}
<h1>Welcome</h1>
<p>This is my webpage</p>
</body>
</html>
Chạy ví dụ »Định nghĩa và cách sử dụng
Thẻ include
cho phép bạn bao gồm nội dung từ một mẫu khác.
Đặt thẻ include
chính xác vào nơi bạn muốn hiển thị nội dung.
Điều này rất hữu ích khi bạn có cùng một nội dung cho nhiều trang.
Bạn cũng có thể gửi các biến vào mẫu bằng cách sử dụng từ khóa with
:
Ví dụ
Nếu tệp bao gồm trông như thế này:
<div>HOME | {{ me }} | ABOUT | FORUM | {{ sponsor }}</div>
Mẫu có thể gửi các giá trị biến vào phần bao gồm như thế này:
<!DOCTYPE html>
<html>
<body>
{% include mymenu.html with me="ALEXANDER" sponsor="W3SCHOOLS" %}
<h1>Welcome</h1>
<p>This is my webpage</p>
</body>
</html>
Chạy ví dụ »Cú pháp
{% include template %}
hoặc
{% include template with key = value %}
Thông số
Value | Description |
---|---|
template | Required. The filename of the template. Either a string or a variable. |
key=value | Optional. A variable name and value to send into the include file. Used
together with the with keyword. You can
have as many key/value pairs as you like. |