Hugo 模板系統詳解
掌握 Hugo 的 Go 模板語法和最佳實踐
Hugo 模板基礎
Hugo 使用 Go 的 text/template 和 html/template 函式庫作為模板引擎。
基本語法
{{ .Title }}
{{ .Content }}
{{ range .Pages }}
<h2>{{ .Title }}</h2>
{{ end }}
模板類型
- baseof.html - 基礎模板
- list.html - 列表頁模板
- single.html - 單頁模板
- partials/ - 可複用元件
條件語句
{{ if .Params.featured }}
<span class="badge">Featured</span>
{{ end }}
迴圈
{{ range .Site.RegularPages }}
<article>{{ .Title }}</article>
{{ end }}