Hugo 預設以 list.html 提供文章清單 Template,並搭配 _list.scss 與 _list.js。
Version
Hugo 0.147.8
List Template
layouts/list.html
{{ define "main" }}
{{ partial "header.html" . }}
<main class="list">
<h1>{{ .Title }}</h1>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .RelPermalink }}">
{{ .Title }}
</a>
</li>
{{ end }}
</ul>
</main>
{{ partial "footer.html" . }}
{{ end }}
- 為 markdown 清單建立
list.html
Line 1
{{ define "main" }}
{{ end }}
- 配合
baseof.html定義mainblock
Line 2
{{ partial "header.html" . }}
- 引用
headerpartial
Line 3
<main class="list">
<h1>{{ .Title }}</h1>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .RelPermalink }}">
{{ .Title }}
</a>
</li>
{{ end }}
</ul>
</main>
list.html主要內容都放在<main>底下,且搭配listclass
Line 15
{{ partial "footer.html" . }}
- 引用
footerpartial
Baseof Template
layouts/baseof.html
<!doctype html>
<html lang="en">
<head>
{{ partial "css.html" . }}
</head>
<body>
{{ block "main" . }}{{ end }}
{{ partial "js.html" . }}
</body>
</html>
- 所有 template 基礎的
baseof.html
Line 6
<body>
{{ block "main" . }}{{ end }}
</body>
- 在
<body>內引用mainblock
List Style
assets/scss/_list.scss
.list {
padding: 2rem;
ul {
display: flex;
flex-direction: column;
gap: 1rem;
}
}
- 搭配
list.html的 SCSS - 使用
巢狀寫法與 HTML 結構同步方便維護
List Script
assets/js/_list.js
export const initList = () => {
console.log('initList()')
}
- 搭配
list.html的 JavaScript
Conclusion
- 無論在
layouts下或themes下,都可使用list.html為文章清單 template list.html相當精簡,只包含headerpartial、footerpartial 與mainblockassets/scss與layouts下的目錄架構完全相同assets/js與layouts下的目錄架構完全相同list.html對應_list.scss與_list.js方便維護,不必將所有 SCSS 與 JavaScript 都寫在main.scss與main.js內