[jade]Sự kế thừa template.
Như ta đã biết jade là một template engine mạnh mẽ của node.js. Jade hỗ trợ sự kế thừa template thông qua các keywords như "block" và "extends". Một block có thể được thay thế trong một template con. Những khối block của Jade có thể cung cấp các nội dung mặc định .
Ví dụ một trang layout.jade như sau:
layout.jade
html
head
title My Site - #{title}
block scripts
script(src='/jquery.js')
body
block content
block foot
#footer
p some footer content
Bây giờ chúng ta muốn kế thừa phần layout ta chỉ cần tạo một fil mới và dùng keyword "extends " . Bây giờ bạn có thể định nghĩa một hoặc nhiều block có thể đè lên nội dung block cha.
page1.jade
extends ./layout.jade
block scripts
script(src='/jquery.js')
script(src='/pets.js')
block content
h1= title
each pet in pets
include pet
Chú ý là ở đây block foot vẫn là block foot của trang layout.jadeMột block có thể được ghi đè bởi những block khác, như ví dụ dưới:
sub-layout.jade
extends ./layout.jade
block content
.sidebar
block sidebar
p nothing
.primary
block primary
p nothing
Và trang con có thể ghi đè:
page2.jade
extends ./sub-layout.jade
block content
.sidebar
block sidebar
p nothing
.primary
block primary
p nothing
Ví dụ ta có một file sau :
html
head
block head
script(src='/vendor/jquery.js')
script(src='/vendor/caustic.js')
body
block content
Giờ ta muốn thêm vào 2 file javascript nữa:extends layout
block append head
script(src='/vendor/three.js')
script(src='/game.js')
hoặcextends layout
append head
script(src='/vendor/three.js')
script(src='/game.js')
Chi tiết : www.manageitservice247.com/blog
Nhận xét
Đăng nhận xét