· HugoWind 團隊 · 技術 · 1 min read · 61 words

Hugo 模板系統詳解

掌握 Hugo 的 Go 模板語法和最佳實踐

Hugo 模板基礎

Hugo 使用 Go 的 text/templatehtml/template 函式庫作為模板引擎。

基本語法

{{ .Title }}
{{ .Content }}
{{ range .Pages }}
  <h2>{{ .Title }}</h2>
{{ end }}

模板類型

  1. baseof.html - 基礎模板
  2. list.html - 列表頁模板
  3. single.html - 單頁模板
  4. partials/ - 可複用元件

條件語句

{{ if .Params.featured }}
  <span class="badge">Featured</span>
{{ end }}

迴圈

{{ range .Site.RegularPages }}
  <article>{{ .Title }}</article>
{{ end }}