Hugo 預設以 single.html 提供單筆文章 Template,並搭配 _single.scss 與 _single.js。
Version
Hugo 0.147.8
Single Template
layouts/single.html
{{ define "main" }}
{{ partial "header.html" . }}
<main class="single">
<h1>{{ .Title }}</h1>
<article>
{{ .Content }}
</article>
</main>
{{ partial "footer.html" . }}
{{ end }}
- 為單筆 markdown 建立
single.html
Line 1
{{ define "main" }}
{{ end }}
- 配合
baseof.html定義mainblock
Line2
{{ partial "header.html" . }}
- 引用
headerpartial
Line 3
<main class="single">
<h1>{{ .Title }}</h1>
<article>
{{ .Content }}
</article>
</main>
single.html主要內容都放在<main>底下,且搭配singleclass
Line 9
{{ 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
Single Style
assets/scss/_single.scss
.single {
margin: 2rem auto;
padding: 1rem;
line-height: 1.8;
max-width: 720px;
}
- 搭配
single.html的 SCSS
Single Script
assets/js/_single.js
export const initSingle = () => {
console.log('initSingle()')
}
- 搭配
single.html的 JavaScript
Conclusion
- 無論在
layouts下或themes下,都可使用single.html為單筆文章 template single.html相當精簡,只包含headerpartial、footerpartial 與mainblockassets/scss與layouts下的目錄架構完全相同assets/js與layouts下的目錄架構完全相同single.html對應_single.scss與_single.js方便維護,不必將所有 SCSS 與 JavaScript 都寫在main.scss與main.js內